i915_gem.c 135 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281
  1. /*
  2. * Copyright © 2008-2015 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. * IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Eric Anholt <eric@anholt.net>
  25. *
  26. */
  27. #include <drm/drmP.h>
  28. #include <drm/drm_vma_manager.h>
  29. #include <drm/i915_drm.h>
  30. #include "i915_drv.h"
  31. #include "i915_vgpu.h"
  32. #include "i915_trace.h"
  33. #include "intel_drv.h"
  34. #include <linux/shmem_fs.h>
  35. #include <linux/slab.h>
  36. #include <linux/swap.h>
  37. #include <linux/pci.h>
  38. #include <linux/dma-buf.h>
  39. #define RQ_BUG_ON(expr)
  40. static void i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj);
  41. static void i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj);
  42. static void
  43. i915_gem_object_retire__write(struct drm_i915_gem_object *obj);
  44. static void
  45. i915_gem_object_retire__read(struct drm_i915_gem_object *obj, int ring);
  46. static bool cpu_cache_is_coherent(struct drm_device *dev,
  47. enum i915_cache_level level)
  48. {
  49. return HAS_LLC(dev) || level != I915_CACHE_NONE;
  50. }
  51. static bool cpu_write_needs_clflush(struct drm_i915_gem_object *obj)
  52. {
  53. if (!cpu_cache_is_coherent(obj->base.dev, obj->cache_level))
  54. return true;
  55. return obj->pin_display;
  56. }
  57. /* some bookkeeping */
  58. static void i915_gem_info_add_obj(struct drm_i915_private *dev_priv,
  59. size_t size)
  60. {
  61. spin_lock(&dev_priv->mm.object_stat_lock);
  62. dev_priv->mm.object_count++;
  63. dev_priv->mm.object_memory += size;
  64. spin_unlock(&dev_priv->mm.object_stat_lock);
  65. }
  66. static void i915_gem_info_remove_obj(struct drm_i915_private *dev_priv,
  67. size_t size)
  68. {
  69. spin_lock(&dev_priv->mm.object_stat_lock);
  70. dev_priv->mm.object_count--;
  71. dev_priv->mm.object_memory -= size;
  72. spin_unlock(&dev_priv->mm.object_stat_lock);
  73. }
  74. static int
  75. i915_gem_wait_for_error(struct i915_gpu_error *error)
  76. {
  77. int ret;
  78. #define EXIT_COND (!i915_reset_in_progress(error) || \
  79. i915_terminally_wedged(error))
  80. if (EXIT_COND)
  81. return 0;
  82. /*
  83. * Only wait 10 seconds for the gpu reset to complete to avoid hanging
  84. * userspace. If it takes that long something really bad is going on and
  85. * we should simply try to bail out and fail as gracefully as possible.
  86. */
  87. ret = wait_event_interruptible_timeout(error->reset_queue,
  88. EXIT_COND,
  89. 10*HZ);
  90. if (ret == 0) {
  91. DRM_ERROR("Timed out waiting for the gpu reset to complete\n");
  92. return -EIO;
  93. } else if (ret < 0) {
  94. return ret;
  95. }
  96. #undef EXIT_COND
  97. return 0;
  98. }
  99. int i915_mutex_lock_interruptible(struct drm_device *dev)
  100. {
  101. struct drm_i915_private *dev_priv = dev->dev_private;
  102. int ret;
  103. ret = i915_gem_wait_for_error(&dev_priv->gpu_error);
  104. if (ret)
  105. return ret;
  106. ret = mutex_lock_interruptible(&dev->struct_mutex);
  107. if (ret)
  108. return ret;
  109. WARN_ON(i915_verify_lists(dev));
  110. return 0;
  111. }
  112. int
  113. i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
  114. struct drm_file *file)
  115. {
  116. struct drm_i915_private *dev_priv = dev->dev_private;
  117. struct drm_i915_gem_get_aperture *args = data;
  118. struct i915_gtt *ggtt = &dev_priv->gtt;
  119. struct i915_vma *vma;
  120. size_t pinned;
  121. pinned = 0;
  122. mutex_lock(&dev->struct_mutex);
  123. list_for_each_entry(vma, &ggtt->base.active_list, mm_list)
  124. if (vma->pin_count)
  125. pinned += vma->node.size;
  126. list_for_each_entry(vma, &ggtt->base.inactive_list, mm_list)
  127. if (vma->pin_count)
  128. pinned += vma->node.size;
  129. mutex_unlock(&dev->struct_mutex);
  130. args->aper_size = dev_priv->gtt.base.total;
  131. args->aper_available_size = args->aper_size - pinned;
  132. return 0;
  133. }
  134. static int
  135. i915_gem_object_get_pages_phys(struct drm_i915_gem_object *obj)
  136. {
  137. struct address_space *mapping = file_inode(obj->base.filp)->i_mapping;
  138. char *vaddr = obj->phys_handle->vaddr;
  139. struct sg_table *st;
  140. struct scatterlist *sg;
  141. int i;
  142. if (WARN_ON(i915_gem_object_needs_bit17_swizzle(obj)))
  143. return -EINVAL;
  144. for (i = 0; i < obj->base.size / PAGE_SIZE; i++) {
  145. struct page *page;
  146. char *src;
  147. page = shmem_read_mapping_page(mapping, i);
  148. if (IS_ERR(page))
  149. return PTR_ERR(page);
  150. src = kmap_atomic(page);
  151. memcpy(vaddr, src, PAGE_SIZE);
  152. drm_clflush_virt_range(vaddr, PAGE_SIZE);
  153. kunmap_atomic(src);
  154. page_cache_release(page);
  155. vaddr += PAGE_SIZE;
  156. }
  157. i915_gem_chipset_flush(obj->base.dev);
  158. st = kmalloc(sizeof(*st), GFP_KERNEL);
  159. if (st == NULL)
  160. return -ENOMEM;
  161. if (sg_alloc_table(st, 1, GFP_KERNEL)) {
  162. kfree(st);
  163. return -ENOMEM;
  164. }
  165. sg = st->sgl;
  166. sg->offset = 0;
  167. sg->length = obj->base.size;
  168. sg_dma_address(sg) = obj->phys_handle->busaddr;
  169. sg_dma_len(sg) = obj->base.size;
  170. obj->pages = st;
  171. return 0;
  172. }
  173. static void
  174. i915_gem_object_put_pages_phys(struct drm_i915_gem_object *obj)
  175. {
  176. int ret;
  177. BUG_ON(obj->madv == __I915_MADV_PURGED);
  178. ret = i915_gem_object_set_to_cpu_domain(obj, true);
  179. if (ret) {
  180. /* In the event of a disaster, abandon all caches and
  181. * hope for the best.
  182. */
  183. WARN_ON(ret != -EIO);
  184. obj->base.read_domains = obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  185. }
  186. if (obj->madv == I915_MADV_DONTNEED)
  187. obj->dirty = 0;
  188. if (obj->dirty) {
  189. struct address_space *mapping = file_inode(obj->base.filp)->i_mapping;
  190. char *vaddr = obj->phys_handle->vaddr;
  191. int i;
  192. for (i = 0; i < obj->base.size / PAGE_SIZE; i++) {
  193. struct page *page;
  194. char *dst;
  195. page = shmem_read_mapping_page(mapping, i);
  196. if (IS_ERR(page))
  197. continue;
  198. dst = kmap_atomic(page);
  199. drm_clflush_virt_range(vaddr, PAGE_SIZE);
  200. memcpy(dst, vaddr, PAGE_SIZE);
  201. kunmap_atomic(dst);
  202. set_page_dirty(page);
  203. if (obj->madv == I915_MADV_WILLNEED)
  204. mark_page_accessed(page);
  205. page_cache_release(page);
  206. vaddr += PAGE_SIZE;
  207. }
  208. obj->dirty = 0;
  209. }
  210. sg_free_table(obj->pages);
  211. kfree(obj->pages);
  212. }
  213. static void
  214. i915_gem_object_release_phys(struct drm_i915_gem_object *obj)
  215. {
  216. drm_pci_free(obj->base.dev, obj->phys_handle);
  217. }
  218. static const struct drm_i915_gem_object_ops i915_gem_phys_ops = {
  219. .get_pages = i915_gem_object_get_pages_phys,
  220. .put_pages = i915_gem_object_put_pages_phys,
  221. .release = i915_gem_object_release_phys,
  222. };
  223. static int
  224. drop_pages(struct drm_i915_gem_object *obj)
  225. {
  226. struct i915_vma *vma, *next;
  227. int ret;
  228. drm_gem_object_reference(&obj->base);
  229. list_for_each_entry_safe(vma, next, &obj->vma_list, vma_link)
  230. if (i915_vma_unbind(vma))
  231. break;
  232. ret = i915_gem_object_put_pages(obj);
  233. drm_gem_object_unreference(&obj->base);
  234. return ret;
  235. }
  236. int
  237. i915_gem_object_attach_phys(struct drm_i915_gem_object *obj,
  238. int align)
  239. {
  240. drm_dma_handle_t *phys;
  241. int ret;
  242. if (obj->phys_handle) {
  243. if ((unsigned long)obj->phys_handle->vaddr & (align -1))
  244. return -EBUSY;
  245. return 0;
  246. }
  247. if (obj->madv != I915_MADV_WILLNEED)
  248. return -EFAULT;
  249. if (obj->base.filp == NULL)
  250. return -EINVAL;
  251. ret = drop_pages(obj);
  252. if (ret)
  253. return ret;
  254. /* create a new object */
  255. phys = drm_pci_alloc(obj->base.dev, obj->base.size, align);
  256. if (!phys)
  257. return -ENOMEM;
  258. obj->phys_handle = phys;
  259. obj->ops = &i915_gem_phys_ops;
  260. return i915_gem_object_get_pages(obj);
  261. }
  262. static int
  263. i915_gem_phys_pwrite(struct drm_i915_gem_object *obj,
  264. struct drm_i915_gem_pwrite *args,
  265. struct drm_file *file_priv)
  266. {
  267. struct drm_device *dev = obj->base.dev;
  268. void *vaddr = obj->phys_handle->vaddr + args->offset;
  269. char __user *user_data = to_user_ptr(args->data_ptr);
  270. int ret = 0;
  271. /* We manually control the domain here and pretend that it
  272. * remains coherent i.e. in the GTT domain, like shmem_pwrite.
  273. */
  274. ret = i915_gem_object_wait_rendering(obj, false);
  275. if (ret)
  276. return ret;
  277. intel_fb_obj_invalidate(obj, ORIGIN_CPU);
  278. if (__copy_from_user_inatomic_nocache(vaddr, user_data, args->size)) {
  279. unsigned long unwritten;
  280. /* The physical object once assigned is fixed for the lifetime
  281. * of the obj, so we can safely drop the lock and continue
  282. * to access vaddr.
  283. */
  284. mutex_unlock(&dev->struct_mutex);
  285. unwritten = copy_from_user(vaddr, user_data, args->size);
  286. mutex_lock(&dev->struct_mutex);
  287. if (unwritten) {
  288. ret = -EFAULT;
  289. goto out;
  290. }
  291. }
  292. drm_clflush_virt_range(vaddr, args->size);
  293. i915_gem_chipset_flush(dev);
  294. out:
  295. intel_fb_obj_flush(obj, false, ORIGIN_CPU);
  296. return ret;
  297. }
  298. void *i915_gem_object_alloc(struct drm_device *dev)
  299. {
  300. struct drm_i915_private *dev_priv = dev->dev_private;
  301. return kmem_cache_zalloc(dev_priv->objects, GFP_KERNEL);
  302. }
  303. void i915_gem_object_free(struct drm_i915_gem_object *obj)
  304. {
  305. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  306. kmem_cache_free(dev_priv->objects, obj);
  307. }
  308. static int
  309. i915_gem_create(struct drm_file *file,
  310. struct drm_device *dev,
  311. uint64_t size,
  312. uint32_t *handle_p)
  313. {
  314. struct drm_i915_gem_object *obj;
  315. int ret;
  316. u32 handle;
  317. size = roundup(size, PAGE_SIZE);
  318. if (size == 0)
  319. return -EINVAL;
  320. /* Allocate the new object */
  321. obj = i915_gem_alloc_object(dev, size);
  322. if (obj == NULL)
  323. return -ENOMEM;
  324. ret = drm_gem_handle_create(file, &obj->base, &handle);
  325. /* drop reference from allocate - handle holds it now */
  326. drm_gem_object_unreference_unlocked(&obj->base);
  327. if (ret)
  328. return ret;
  329. *handle_p = handle;
  330. return 0;
  331. }
  332. int
  333. i915_gem_dumb_create(struct drm_file *file,
  334. struct drm_device *dev,
  335. struct drm_mode_create_dumb *args)
  336. {
  337. /* have to work out size/pitch and return them */
  338. args->pitch = ALIGN(args->width * DIV_ROUND_UP(args->bpp, 8), 64);
  339. args->size = args->pitch * args->height;
  340. return i915_gem_create(file, dev,
  341. args->size, &args->handle);
  342. }
  343. /**
  344. * Creates a new mm object and returns a handle to it.
  345. */
  346. int
  347. i915_gem_create_ioctl(struct drm_device *dev, void *data,
  348. struct drm_file *file)
  349. {
  350. struct drm_i915_gem_create *args = data;
  351. return i915_gem_create(file, dev,
  352. args->size, &args->handle);
  353. }
  354. static inline int
  355. __copy_to_user_swizzled(char __user *cpu_vaddr,
  356. const char *gpu_vaddr, int gpu_offset,
  357. int length)
  358. {
  359. int ret, cpu_offset = 0;
  360. while (length > 0) {
  361. int cacheline_end = ALIGN(gpu_offset + 1, 64);
  362. int this_length = min(cacheline_end - gpu_offset, length);
  363. int swizzled_gpu_offset = gpu_offset ^ 64;
  364. ret = __copy_to_user(cpu_vaddr + cpu_offset,
  365. gpu_vaddr + swizzled_gpu_offset,
  366. this_length);
  367. if (ret)
  368. return ret + length;
  369. cpu_offset += this_length;
  370. gpu_offset += this_length;
  371. length -= this_length;
  372. }
  373. return 0;
  374. }
  375. static inline int
  376. __copy_from_user_swizzled(char *gpu_vaddr, int gpu_offset,
  377. const char __user *cpu_vaddr,
  378. int length)
  379. {
  380. int ret, cpu_offset = 0;
  381. while (length > 0) {
  382. int cacheline_end = ALIGN(gpu_offset + 1, 64);
  383. int this_length = min(cacheline_end - gpu_offset, length);
  384. int swizzled_gpu_offset = gpu_offset ^ 64;
  385. ret = __copy_from_user(gpu_vaddr + swizzled_gpu_offset,
  386. cpu_vaddr + cpu_offset,
  387. this_length);
  388. if (ret)
  389. return ret + length;
  390. cpu_offset += this_length;
  391. gpu_offset += this_length;
  392. length -= this_length;
  393. }
  394. return 0;
  395. }
  396. /*
  397. * Pins the specified object's pages and synchronizes the object with
  398. * GPU accesses. Sets needs_clflush to non-zero if the caller should
  399. * flush the object from the CPU cache.
  400. */
  401. int i915_gem_obj_prepare_shmem_read(struct drm_i915_gem_object *obj,
  402. int *needs_clflush)
  403. {
  404. int ret;
  405. *needs_clflush = 0;
  406. if (!obj->base.filp)
  407. return -EINVAL;
  408. if (!(obj->base.read_domains & I915_GEM_DOMAIN_CPU)) {
  409. /* If we're not in the cpu read domain, set ourself into the gtt
  410. * read domain and manually flush cachelines (if required). This
  411. * optimizes for the case when the gpu will dirty the data
  412. * anyway again before the next pread happens. */
  413. *needs_clflush = !cpu_cache_is_coherent(obj->base.dev,
  414. obj->cache_level);
  415. ret = i915_gem_object_wait_rendering(obj, true);
  416. if (ret)
  417. return ret;
  418. }
  419. ret = i915_gem_object_get_pages(obj);
  420. if (ret)
  421. return ret;
  422. i915_gem_object_pin_pages(obj);
  423. return ret;
  424. }
  425. /* Per-page copy function for the shmem pread fastpath.
  426. * Flushes invalid cachelines before reading the target if
  427. * needs_clflush is set. */
  428. static int
  429. shmem_pread_fast(struct page *page, int shmem_page_offset, int page_length,
  430. char __user *user_data,
  431. bool page_do_bit17_swizzling, bool needs_clflush)
  432. {
  433. char *vaddr;
  434. int ret;
  435. if (unlikely(page_do_bit17_swizzling))
  436. return -EINVAL;
  437. vaddr = kmap_atomic(page);
  438. if (needs_clflush)
  439. drm_clflush_virt_range(vaddr + shmem_page_offset,
  440. page_length);
  441. ret = __copy_to_user_inatomic(user_data,
  442. vaddr + shmem_page_offset,
  443. page_length);
  444. kunmap_atomic(vaddr);
  445. return ret ? -EFAULT : 0;
  446. }
  447. static void
  448. shmem_clflush_swizzled_range(char *addr, unsigned long length,
  449. bool swizzled)
  450. {
  451. if (unlikely(swizzled)) {
  452. unsigned long start = (unsigned long) addr;
  453. unsigned long end = (unsigned long) addr + length;
  454. /* For swizzling simply ensure that we always flush both
  455. * channels. Lame, but simple and it works. Swizzled
  456. * pwrite/pread is far from a hotpath - current userspace
  457. * doesn't use it at all. */
  458. start = round_down(start, 128);
  459. end = round_up(end, 128);
  460. drm_clflush_virt_range((void *)start, end - start);
  461. } else {
  462. drm_clflush_virt_range(addr, length);
  463. }
  464. }
  465. /* Only difference to the fast-path function is that this can handle bit17
  466. * and uses non-atomic copy and kmap functions. */
  467. static int
  468. shmem_pread_slow(struct page *page, int shmem_page_offset, int page_length,
  469. char __user *user_data,
  470. bool page_do_bit17_swizzling, bool needs_clflush)
  471. {
  472. char *vaddr;
  473. int ret;
  474. vaddr = kmap(page);
  475. if (needs_clflush)
  476. shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
  477. page_length,
  478. page_do_bit17_swizzling);
  479. if (page_do_bit17_swizzling)
  480. ret = __copy_to_user_swizzled(user_data,
  481. vaddr, shmem_page_offset,
  482. page_length);
  483. else
  484. ret = __copy_to_user(user_data,
  485. vaddr + shmem_page_offset,
  486. page_length);
  487. kunmap(page);
  488. return ret ? - EFAULT : 0;
  489. }
  490. static int
  491. i915_gem_shmem_pread(struct drm_device *dev,
  492. struct drm_i915_gem_object *obj,
  493. struct drm_i915_gem_pread *args,
  494. struct drm_file *file)
  495. {
  496. char __user *user_data;
  497. ssize_t remain;
  498. loff_t offset;
  499. int shmem_page_offset, page_length, ret = 0;
  500. int obj_do_bit17_swizzling, page_do_bit17_swizzling;
  501. int prefaulted = 0;
  502. int needs_clflush = 0;
  503. struct sg_page_iter sg_iter;
  504. user_data = to_user_ptr(args->data_ptr);
  505. remain = args->size;
  506. obj_do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
  507. ret = i915_gem_obj_prepare_shmem_read(obj, &needs_clflush);
  508. if (ret)
  509. return ret;
  510. offset = args->offset;
  511. for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents,
  512. offset >> PAGE_SHIFT) {
  513. struct page *page = sg_page_iter_page(&sg_iter);
  514. if (remain <= 0)
  515. break;
  516. /* Operation in this page
  517. *
  518. * shmem_page_offset = offset within page in shmem file
  519. * page_length = bytes to copy for this page
  520. */
  521. shmem_page_offset = offset_in_page(offset);
  522. page_length = remain;
  523. if ((shmem_page_offset + page_length) > PAGE_SIZE)
  524. page_length = PAGE_SIZE - shmem_page_offset;
  525. page_do_bit17_swizzling = obj_do_bit17_swizzling &&
  526. (page_to_phys(page) & (1 << 17)) != 0;
  527. ret = shmem_pread_fast(page, shmem_page_offset, page_length,
  528. user_data, page_do_bit17_swizzling,
  529. needs_clflush);
  530. if (ret == 0)
  531. goto next_page;
  532. mutex_unlock(&dev->struct_mutex);
  533. if (likely(!i915.prefault_disable) && !prefaulted) {
  534. ret = fault_in_multipages_writeable(user_data, remain);
  535. /* Userspace is tricking us, but we've already clobbered
  536. * its pages with the prefault and promised to write the
  537. * data up to the first fault. Hence ignore any errors
  538. * and just continue. */
  539. (void)ret;
  540. prefaulted = 1;
  541. }
  542. ret = shmem_pread_slow(page, shmem_page_offset, page_length,
  543. user_data, page_do_bit17_swizzling,
  544. needs_clflush);
  545. mutex_lock(&dev->struct_mutex);
  546. if (ret)
  547. goto out;
  548. next_page:
  549. remain -= page_length;
  550. user_data += page_length;
  551. offset += page_length;
  552. }
  553. out:
  554. i915_gem_object_unpin_pages(obj);
  555. return ret;
  556. }
  557. /**
  558. * Reads data from the object referenced by handle.
  559. *
  560. * On error, the contents of *data are undefined.
  561. */
  562. int
  563. i915_gem_pread_ioctl(struct drm_device *dev, void *data,
  564. struct drm_file *file)
  565. {
  566. struct drm_i915_gem_pread *args = data;
  567. struct drm_i915_gem_object *obj;
  568. int ret = 0;
  569. if (args->size == 0)
  570. return 0;
  571. if (!access_ok(VERIFY_WRITE,
  572. to_user_ptr(args->data_ptr),
  573. args->size))
  574. return -EFAULT;
  575. ret = i915_mutex_lock_interruptible(dev);
  576. if (ret)
  577. return ret;
  578. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  579. if (&obj->base == NULL) {
  580. ret = -ENOENT;
  581. goto unlock;
  582. }
  583. /* Bounds check source. */
  584. if (args->offset > obj->base.size ||
  585. args->size > obj->base.size - args->offset) {
  586. ret = -EINVAL;
  587. goto out;
  588. }
  589. /* prime objects have no backing filp to GEM pread/pwrite
  590. * pages from.
  591. */
  592. if (!obj->base.filp) {
  593. ret = -EINVAL;
  594. goto out;
  595. }
  596. trace_i915_gem_object_pread(obj, args->offset, args->size);
  597. ret = i915_gem_shmem_pread(dev, obj, args, file);
  598. out:
  599. drm_gem_object_unreference(&obj->base);
  600. unlock:
  601. mutex_unlock(&dev->struct_mutex);
  602. return ret;
  603. }
  604. /* This is the fast write path which cannot handle
  605. * page faults in the source data
  606. */
  607. static inline int
  608. fast_user_write(struct io_mapping *mapping,
  609. loff_t page_base, int page_offset,
  610. char __user *user_data,
  611. int length)
  612. {
  613. void __iomem *vaddr_atomic;
  614. void *vaddr;
  615. unsigned long unwritten;
  616. vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base);
  617. /* We can use the cpu mem copy function because this is X86. */
  618. vaddr = (void __force*)vaddr_atomic + page_offset;
  619. unwritten = __copy_from_user_inatomic_nocache(vaddr,
  620. user_data, length);
  621. io_mapping_unmap_atomic(vaddr_atomic);
  622. return unwritten;
  623. }
  624. /**
  625. * This is the fast pwrite path, where we copy the data directly from the
  626. * user into the GTT, uncached.
  627. */
  628. static int
  629. i915_gem_gtt_pwrite_fast(struct drm_device *dev,
  630. struct drm_i915_gem_object *obj,
  631. struct drm_i915_gem_pwrite *args,
  632. struct drm_file *file)
  633. {
  634. struct drm_i915_private *dev_priv = dev->dev_private;
  635. ssize_t remain;
  636. loff_t offset, page_base;
  637. char __user *user_data;
  638. int page_offset, page_length, ret;
  639. ret = i915_gem_obj_ggtt_pin(obj, 0, PIN_MAPPABLE | PIN_NONBLOCK);
  640. if (ret)
  641. goto out;
  642. ret = i915_gem_object_set_to_gtt_domain(obj, true);
  643. if (ret)
  644. goto out_unpin;
  645. ret = i915_gem_object_put_fence(obj);
  646. if (ret)
  647. goto out_unpin;
  648. user_data = to_user_ptr(args->data_ptr);
  649. remain = args->size;
  650. offset = i915_gem_obj_ggtt_offset(obj) + args->offset;
  651. intel_fb_obj_invalidate(obj, ORIGIN_GTT);
  652. while (remain > 0) {
  653. /* Operation in this page
  654. *
  655. * page_base = page offset within aperture
  656. * page_offset = offset within page
  657. * page_length = bytes to copy for this page
  658. */
  659. page_base = offset & PAGE_MASK;
  660. page_offset = offset_in_page(offset);
  661. page_length = remain;
  662. if ((page_offset + remain) > PAGE_SIZE)
  663. page_length = PAGE_SIZE - page_offset;
  664. /* If we get a fault while copying data, then (presumably) our
  665. * source page isn't available. Return the error and we'll
  666. * retry in the slow path.
  667. */
  668. if (fast_user_write(dev_priv->gtt.mappable, page_base,
  669. page_offset, user_data, page_length)) {
  670. ret = -EFAULT;
  671. goto out_flush;
  672. }
  673. remain -= page_length;
  674. user_data += page_length;
  675. offset += page_length;
  676. }
  677. out_flush:
  678. intel_fb_obj_flush(obj, false, ORIGIN_GTT);
  679. out_unpin:
  680. i915_gem_object_ggtt_unpin(obj);
  681. out:
  682. return ret;
  683. }
  684. /* Per-page copy function for the shmem pwrite fastpath.
  685. * Flushes invalid cachelines before writing to the target if
  686. * needs_clflush_before is set and flushes out any written cachelines after
  687. * writing if needs_clflush is set. */
  688. static int
  689. shmem_pwrite_fast(struct page *page, int shmem_page_offset, int page_length,
  690. char __user *user_data,
  691. bool page_do_bit17_swizzling,
  692. bool needs_clflush_before,
  693. bool needs_clflush_after)
  694. {
  695. char *vaddr;
  696. int ret;
  697. if (unlikely(page_do_bit17_swizzling))
  698. return -EINVAL;
  699. vaddr = kmap_atomic(page);
  700. if (needs_clflush_before)
  701. drm_clflush_virt_range(vaddr + shmem_page_offset,
  702. page_length);
  703. ret = __copy_from_user_inatomic(vaddr + shmem_page_offset,
  704. user_data, page_length);
  705. if (needs_clflush_after)
  706. drm_clflush_virt_range(vaddr + shmem_page_offset,
  707. page_length);
  708. kunmap_atomic(vaddr);
  709. return ret ? -EFAULT : 0;
  710. }
  711. /* Only difference to the fast-path function is that this can handle bit17
  712. * and uses non-atomic copy and kmap functions. */
  713. static int
  714. shmem_pwrite_slow(struct page *page, int shmem_page_offset, int page_length,
  715. char __user *user_data,
  716. bool page_do_bit17_swizzling,
  717. bool needs_clflush_before,
  718. bool needs_clflush_after)
  719. {
  720. char *vaddr;
  721. int ret;
  722. vaddr = kmap(page);
  723. if (unlikely(needs_clflush_before || page_do_bit17_swizzling))
  724. shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
  725. page_length,
  726. page_do_bit17_swizzling);
  727. if (page_do_bit17_swizzling)
  728. ret = __copy_from_user_swizzled(vaddr, shmem_page_offset,
  729. user_data,
  730. page_length);
  731. else
  732. ret = __copy_from_user(vaddr + shmem_page_offset,
  733. user_data,
  734. page_length);
  735. if (needs_clflush_after)
  736. shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
  737. page_length,
  738. page_do_bit17_swizzling);
  739. kunmap(page);
  740. return ret ? -EFAULT : 0;
  741. }
  742. static int
  743. i915_gem_shmem_pwrite(struct drm_device *dev,
  744. struct drm_i915_gem_object *obj,
  745. struct drm_i915_gem_pwrite *args,
  746. struct drm_file *file)
  747. {
  748. ssize_t remain;
  749. loff_t offset;
  750. char __user *user_data;
  751. int shmem_page_offset, page_length, ret = 0;
  752. int obj_do_bit17_swizzling, page_do_bit17_swizzling;
  753. int hit_slowpath = 0;
  754. int needs_clflush_after = 0;
  755. int needs_clflush_before = 0;
  756. struct sg_page_iter sg_iter;
  757. user_data = to_user_ptr(args->data_ptr);
  758. remain = args->size;
  759. obj_do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
  760. if (obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
  761. /* If we're not in the cpu write domain, set ourself into the gtt
  762. * write domain and manually flush cachelines (if required). This
  763. * optimizes for the case when the gpu will use the data
  764. * right away and we therefore have to clflush anyway. */
  765. needs_clflush_after = cpu_write_needs_clflush(obj);
  766. ret = i915_gem_object_wait_rendering(obj, false);
  767. if (ret)
  768. return ret;
  769. }
  770. /* Same trick applies to invalidate partially written cachelines read
  771. * before writing. */
  772. if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0)
  773. needs_clflush_before =
  774. !cpu_cache_is_coherent(dev, obj->cache_level);
  775. ret = i915_gem_object_get_pages(obj);
  776. if (ret)
  777. return ret;
  778. intel_fb_obj_invalidate(obj, ORIGIN_CPU);
  779. i915_gem_object_pin_pages(obj);
  780. offset = args->offset;
  781. obj->dirty = 1;
  782. for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents,
  783. offset >> PAGE_SHIFT) {
  784. struct page *page = sg_page_iter_page(&sg_iter);
  785. int partial_cacheline_write;
  786. if (remain <= 0)
  787. break;
  788. /* Operation in this page
  789. *
  790. * shmem_page_offset = offset within page in shmem file
  791. * page_length = bytes to copy for this page
  792. */
  793. shmem_page_offset = offset_in_page(offset);
  794. page_length = remain;
  795. if ((shmem_page_offset + page_length) > PAGE_SIZE)
  796. page_length = PAGE_SIZE - shmem_page_offset;
  797. /* If we don't overwrite a cacheline completely we need to be
  798. * careful to have up-to-date data by first clflushing. Don't
  799. * overcomplicate things and flush the entire patch. */
  800. partial_cacheline_write = needs_clflush_before &&
  801. ((shmem_page_offset | page_length)
  802. & (boot_cpu_data.x86_clflush_size - 1));
  803. page_do_bit17_swizzling = obj_do_bit17_swizzling &&
  804. (page_to_phys(page) & (1 << 17)) != 0;
  805. ret = shmem_pwrite_fast(page, shmem_page_offset, page_length,
  806. user_data, page_do_bit17_swizzling,
  807. partial_cacheline_write,
  808. needs_clflush_after);
  809. if (ret == 0)
  810. goto next_page;
  811. hit_slowpath = 1;
  812. mutex_unlock(&dev->struct_mutex);
  813. ret = shmem_pwrite_slow(page, shmem_page_offset, page_length,
  814. user_data, page_do_bit17_swizzling,
  815. partial_cacheline_write,
  816. needs_clflush_after);
  817. mutex_lock(&dev->struct_mutex);
  818. if (ret)
  819. goto out;
  820. next_page:
  821. remain -= page_length;
  822. user_data += page_length;
  823. offset += page_length;
  824. }
  825. out:
  826. i915_gem_object_unpin_pages(obj);
  827. if (hit_slowpath) {
  828. /*
  829. * Fixup: Flush cpu caches in case we didn't flush the dirty
  830. * cachelines in-line while writing and the object moved
  831. * out of the cpu write domain while we've dropped the lock.
  832. */
  833. if (!needs_clflush_after &&
  834. obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
  835. if (i915_gem_clflush_object(obj, obj->pin_display))
  836. needs_clflush_after = true;
  837. }
  838. }
  839. if (needs_clflush_after)
  840. i915_gem_chipset_flush(dev);
  841. else
  842. obj->cache_dirty = true;
  843. intel_fb_obj_flush(obj, false, ORIGIN_CPU);
  844. return ret;
  845. }
  846. /**
  847. * Writes data to the object referenced by handle.
  848. *
  849. * On error, the contents of the buffer that were to be modified are undefined.
  850. */
  851. int
  852. i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
  853. struct drm_file *file)
  854. {
  855. struct drm_i915_private *dev_priv = dev->dev_private;
  856. struct drm_i915_gem_pwrite *args = data;
  857. struct drm_i915_gem_object *obj;
  858. int ret;
  859. if (args->size == 0)
  860. return 0;
  861. if (!access_ok(VERIFY_READ,
  862. to_user_ptr(args->data_ptr),
  863. args->size))
  864. return -EFAULT;
  865. if (likely(!i915.prefault_disable)) {
  866. ret = fault_in_multipages_readable(to_user_ptr(args->data_ptr),
  867. args->size);
  868. if (ret)
  869. return -EFAULT;
  870. }
  871. intel_runtime_pm_get(dev_priv);
  872. ret = i915_mutex_lock_interruptible(dev);
  873. if (ret)
  874. goto put_rpm;
  875. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  876. if (&obj->base == NULL) {
  877. ret = -ENOENT;
  878. goto unlock;
  879. }
  880. /* Bounds check destination. */
  881. if (args->offset > obj->base.size ||
  882. args->size > obj->base.size - args->offset) {
  883. ret = -EINVAL;
  884. goto out;
  885. }
  886. /* prime objects have no backing filp to GEM pread/pwrite
  887. * pages from.
  888. */
  889. if (!obj->base.filp) {
  890. ret = -EINVAL;
  891. goto out;
  892. }
  893. trace_i915_gem_object_pwrite(obj, args->offset, args->size);
  894. ret = -EFAULT;
  895. /* We can only do the GTT pwrite on untiled buffers, as otherwise
  896. * it would end up going through the fenced access, and we'll get
  897. * different detiling behavior between reading and writing.
  898. * pread/pwrite currently are reading and writing from the CPU
  899. * perspective, requiring manual detiling by the client.
  900. */
  901. if (obj->tiling_mode == I915_TILING_NONE &&
  902. obj->base.write_domain != I915_GEM_DOMAIN_CPU &&
  903. cpu_write_needs_clflush(obj)) {
  904. ret = i915_gem_gtt_pwrite_fast(dev, obj, args, file);
  905. /* Note that the gtt paths might fail with non-page-backed user
  906. * pointers (e.g. gtt mappings when moving data between
  907. * textures). Fallback to the shmem path in that case. */
  908. }
  909. if (ret == -EFAULT || ret == -ENOSPC) {
  910. if (obj->phys_handle)
  911. ret = i915_gem_phys_pwrite(obj, args, file);
  912. else
  913. ret = i915_gem_shmem_pwrite(dev, obj, args, file);
  914. }
  915. out:
  916. drm_gem_object_unreference(&obj->base);
  917. unlock:
  918. mutex_unlock(&dev->struct_mutex);
  919. put_rpm:
  920. intel_runtime_pm_put(dev_priv);
  921. return ret;
  922. }
  923. int
  924. i915_gem_check_wedge(struct i915_gpu_error *error,
  925. bool interruptible)
  926. {
  927. if (i915_reset_in_progress(error)) {
  928. /* Non-interruptible callers can't handle -EAGAIN, hence return
  929. * -EIO unconditionally for these. */
  930. if (!interruptible)
  931. return -EIO;
  932. /* Recovery complete, but the reset failed ... */
  933. if (i915_terminally_wedged(error))
  934. return -EIO;
  935. /*
  936. * Check if GPU Reset is in progress - we need intel_ring_begin
  937. * to work properly to reinit the hw state while the gpu is
  938. * still marked as reset-in-progress. Handle this with a flag.
  939. */
  940. if (!error->reload_in_reset)
  941. return -EAGAIN;
  942. }
  943. return 0;
  944. }
  945. static void fake_irq(unsigned long data)
  946. {
  947. wake_up_process((struct task_struct *)data);
  948. }
  949. static bool missed_irq(struct drm_i915_private *dev_priv,
  950. struct intel_engine_cs *ring)
  951. {
  952. return test_bit(ring->id, &dev_priv->gpu_error.missed_irq_rings);
  953. }
  954. static unsigned long local_clock_us(unsigned *cpu)
  955. {
  956. unsigned long t;
  957. /* Cheaply and approximately convert from nanoseconds to microseconds.
  958. * The result and subsequent calculations are also defined in the same
  959. * approximate microseconds units. The principal source of timing
  960. * error here is from the simple truncation.
  961. *
  962. * Note that local_clock() is only defined wrt to the current CPU;
  963. * the comparisons are no longer valid if we switch CPUs. Instead of
  964. * blocking preemption for the entire busywait, we can detect the CPU
  965. * switch and use that as indicator of system load and a reason to
  966. * stop busywaiting, see busywait_stop().
  967. */
  968. *cpu = get_cpu();
  969. t = local_clock() >> 10;
  970. put_cpu();
  971. return t;
  972. }
  973. static bool busywait_stop(unsigned long timeout, unsigned cpu)
  974. {
  975. unsigned this_cpu;
  976. if (time_after(local_clock_us(&this_cpu), timeout))
  977. return true;
  978. return this_cpu != cpu;
  979. }
  980. static int __i915_spin_request(struct drm_i915_gem_request *req, int state)
  981. {
  982. unsigned long timeout;
  983. unsigned cpu;
  984. /* When waiting for high frequency requests, e.g. during synchronous
  985. * rendering split between the CPU and GPU, the finite amount of time
  986. * required to set up the irq and wait upon it limits the response
  987. * rate. By busywaiting on the request completion for a short while we
  988. * can service the high frequency waits as quick as possible. However,
  989. * if it is a slow request, we want to sleep as quickly as possible.
  990. * The tradeoff between waiting and sleeping is roughly the time it
  991. * takes to sleep on a request, on the order of a microsecond.
  992. */
  993. if (req->ring->irq_refcount)
  994. return -EBUSY;
  995. /* Only spin if we know the GPU is processing this request */
  996. if (!i915_gem_request_started(req, true))
  997. return -EAGAIN;
  998. timeout = local_clock_us(&cpu) + 5;
  999. while (!need_resched()) {
  1000. if (i915_gem_request_completed(req, true))
  1001. return 0;
  1002. if (signal_pending_state(state, current))
  1003. break;
  1004. if (busywait_stop(timeout, cpu))
  1005. break;
  1006. cpu_relax_lowlatency();
  1007. }
  1008. if (i915_gem_request_completed(req, false))
  1009. return 0;
  1010. return -EAGAIN;
  1011. }
  1012. /**
  1013. * __i915_wait_request - wait until execution of request has finished
  1014. * @req: duh!
  1015. * @reset_counter: reset sequence associated with the given request
  1016. * @interruptible: do an interruptible wait (normally yes)
  1017. * @timeout: in - how long to wait (NULL forever); out - how much time remaining
  1018. *
  1019. * Note: It is of utmost importance that the passed in seqno and reset_counter
  1020. * values have been read by the caller in an smp safe manner. Where read-side
  1021. * locks are involved, it is sufficient to read the reset_counter before
  1022. * unlocking the lock that protects the seqno. For lockless tricks, the
  1023. * reset_counter _must_ be read before, and an appropriate smp_rmb must be
  1024. * inserted.
  1025. *
  1026. * Returns 0 if the request was found within the alloted time. Else returns the
  1027. * errno with remaining time filled in timeout argument.
  1028. */
  1029. int __i915_wait_request(struct drm_i915_gem_request *req,
  1030. unsigned reset_counter,
  1031. bool interruptible,
  1032. s64 *timeout,
  1033. struct intel_rps_client *rps)
  1034. {
  1035. struct intel_engine_cs *ring = i915_gem_request_get_ring(req);
  1036. struct drm_device *dev = ring->dev;
  1037. struct drm_i915_private *dev_priv = dev->dev_private;
  1038. const bool irq_test_in_progress =
  1039. ACCESS_ONCE(dev_priv->gpu_error.test_irq_rings) & intel_ring_flag(ring);
  1040. int state = interruptible ? TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE;
  1041. DEFINE_WAIT(wait);
  1042. unsigned long timeout_expire;
  1043. s64 before, now;
  1044. int ret;
  1045. WARN(!intel_irqs_enabled(dev_priv), "IRQs disabled");
  1046. if (list_empty(&req->list))
  1047. return 0;
  1048. if (i915_gem_request_completed(req, true))
  1049. return 0;
  1050. timeout_expire = 0;
  1051. if (timeout) {
  1052. if (WARN_ON(*timeout < 0))
  1053. return -EINVAL;
  1054. if (*timeout == 0)
  1055. return -ETIME;
  1056. timeout_expire = jiffies + nsecs_to_jiffies_timeout(*timeout);
  1057. }
  1058. if (INTEL_INFO(dev_priv)->gen >= 6)
  1059. gen6_rps_boost(dev_priv, rps, req->emitted_jiffies);
  1060. /* Record current time in case interrupted by signal, or wedged */
  1061. trace_i915_gem_request_wait_begin(req);
  1062. before = ktime_get_raw_ns();
  1063. /* Optimistic spin for the next jiffie before touching IRQs */
  1064. ret = __i915_spin_request(req, state);
  1065. if (ret == 0)
  1066. goto out;
  1067. if (!irq_test_in_progress && WARN_ON(!ring->irq_get(ring))) {
  1068. ret = -ENODEV;
  1069. goto out;
  1070. }
  1071. for (;;) {
  1072. struct timer_list timer;
  1073. prepare_to_wait(&ring->irq_queue, &wait, state);
  1074. /* We need to check whether any gpu reset happened in between
  1075. * the caller grabbing the seqno and now ... */
  1076. if (reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter)) {
  1077. /* ... but upgrade the -EAGAIN to an -EIO if the gpu
  1078. * is truely gone. */
  1079. ret = i915_gem_check_wedge(&dev_priv->gpu_error, interruptible);
  1080. if (ret == 0)
  1081. ret = -EAGAIN;
  1082. break;
  1083. }
  1084. if (i915_gem_request_completed(req, false)) {
  1085. ret = 0;
  1086. break;
  1087. }
  1088. if (signal_pending_state(state, current)) {
  1089. ret = -ERESTARTSYS;
  1090. break;
  1091. }
  1092. if (timeout && time_after_eq(jiffies, timeout_expire)) {
  1093. ret = -ETIME;
  1094. break;
  1095. }
  1096. timer.function = NULL;
  1097. if (timeout || missed_irq(dev_priv, ring)) {
  1098. unsigned long expire;
  1099. setup_timer_on_stack(&timer, fake_irq, (unsigned long)current);
  1100. expire = missed_irq(dev_priv, ring) ? jiffies + 1 : timeout_expire;
  1101. mod_timer(&timer, expire);
  1102. }
  1103. io_schedule();
  1104. if (timer.function) {
  1105. del_singleshot_timer_sync(&timer);
  1106. destroy_timer_on_stack(&timer);
  1107. }
  1108. }
  1109. if (!irq_test_in_progress)
  1110. ring->irq_put(ring);
  1111. finish_wait(&ring->irq_queue, &wait);
  1112. out:
  1113. now = ktime_get_raw_ns();
  1114. trace_i915_gem_request_wait_end(req);
  1115. if (timeout) {
  1116. s64 tres = *timeout - (now - before);
  1117. *timeout = tres < 0 ? 0 : tres;
  1118. /*
  1119. * Apparently ktime isn't accurate enough and occasionally has a
  1120. * bit of mismatch in the jiffies<->nsecs<->ktime loop. So patch
  1121. * things up to make the test happy. We allow up to 1 jiffy.
  1122. *
  1123. * This is a regrssion from the timespec->ktime conversion.
  1124. */
  1125. if (ret == -ETIME && *timeout < jiffies_to_usecs(1)*1000)
  1126. *timeout = 0;
  1127. }
  1128. return ret;
  1129. }
  1130. int i915_gem_request_add_to_client(struct drm_i915_gem_request *req,
  1131. struct drm_file *file)
  1132. {
  1133. struct drm_i915_private *dev_private;
  1134. struct drm_i915_file_private *file_priv;
  1135. WARN_ON(!req || !file || req->file_priv);
  1136. if (!req || !file)
  1137. return -EINVAL;
  1138. if (req->file_priv)
  1139. return -EINVAL;
  1140. dev_private = req->ring->dev->dev_private;
  1141. file_priv = file->driver_priv;
  1142. spin_lock(&file_priv->mm.lock);
  1143. req->file_priv = file_priv;
  1144. list_add_tail(&req->client_list, &file_priv->mm.request_list);
  1145. spin_unlock(&file_priv->mm.lock);
  1146. req->pid = get_pid(task_pid(current));
  1147. return 0;
  1148. }
  1149. static inline void
  1150. i915_gem_request_remove_from_client(struct drm_i915_gem_request *request)
  1151. {
  1152. struct drm_i915_file_private *file_priv = request->file_priv;
  1153. if (!file_priv)
  1154. return;
  1155. spin_lock(&file_priv->mm.lock);
  1156. list_del(&request->client_list);
  1157. request->file_priv = NULL;
  1158. spin_unlock(&file_priv->mm.lock);
  1159. put_pid(request->pid);
  1160. request->pid = NULL;
  1161. }
  1162. static void i915_gem_request_retire(struct drm_i915_gem_request *request)
  1163. {
  1164. trace_i915_gem_request_retire(request);
  1165. /* We know the GPU must have read the request to have
  1166. * sent us the seqno + interrupt, so use the position
  1167. * of tail of the request to update the last known position
  1168. * of the GPU head.
  1169. *
  1170. * Note this requires that we are always called in request
  1171. * completion order.
  1172. */
  1173. request->ringbuf->last_retired_head = request->postfix;
  1174. list_del_init(&request->list);
  1175. i915_gem_request_remove_from_client(request);
  1176. i915_gem_request_unreference(request);
  1177. }
  1178. static void
  1179. __i915_gem_request_retire__upto(struct drm_i915_gem_request *req)
  1180. {
  1181. struct intel_engine_cs *engine = req->ring;
  1182. struct drm_i915_gem_request *tmp;
  1183. lockdep_assert_held(&engine->dev->struct_mutex);
  1184. if (list_empty(&req->list))
  1185. return;
  1186. do {
  1187. tmp = list_first_entry(&engine->request_list,
  1188. typeof(*tmp), list);
  1189. i915_gem_request_retire(tmp);
  1190. } while (tmp != req);
  1191. WARN_ON(i915_verify_lists(engine->dev));
  1192. }
  1193. /**
  1194. * Waits for a request to be signaled, and cleans up the
  1195. * request and object lists appropriately for that event.
  1196. */
  1197. int
  1198. i915_wait_request(struct drm_i915_gem_request *req)
  1199. {
  1200. struct drm_device *dev;
  1201. struct drm_i915_private *dev_priv;
  1202. bool interruptible;
  1203. int ret;
  1204. BUG_ON(req == NULL);
  1205. dev = req->ring->dev;
  1206. dev_priv = dev->dev_private;
  1207. interruptible = dev_priv->mm.interruptible;
  1208. BUG_ON(!mutex_is_locked(&dev->struct_mutex));
  1209. ret = i915_gem_check_wedge(&dev_priv->gpu_error, interruptible);
  1210. if (ret)
  1211. return ret;
  1212. ret = __i915_wait_request(req,
  1213. atomic_read(&dev_priv->gpu_error.reset_counter),
  1214. interruptible, NULL, NULL);
  1215. if (ret)
  1216. return ret;
  1217. __i915_gem_request_retire__upto(req);
  1218. return 0;
  1219. }
  1220. /**
  1221. * Ensures that all rendering to the object has completed and the object is
  1222. * safe to unbind from the GTT or access from the CPU.
  1223. */
  1224. int
  1225. i915_gem_object_wait_rendering(struct drm_i915_gem_object *obj,
  1226. bool readonly)
  1227. {
  1228. int ret, i;
  1229. if (!obj->active)
  1230. return 0;
  1231. if (readonly) {
  1232. if (obj->last_write_req != NULL) {
  1233. ret = i915_wait_request(obj->last_write_req);
  1234. if (ret)
  1235. return ret;
  1236. i = obj->last_write_req->ring->id;
  1237. if (obj->last_read_req[i] == obj->last_write_req)
  1238. i915_gem_object_retire__read(obj, i);
  1239. else
  1240. i915_gem_object_retire__write(obj);
  1241. }
  1242. } else {
  1243. for (i = 0; i < I915_NUM_RINGS; i++) {
  1244. if (obj->last_read_req[i] == NULL)
  1245. continue;
  1246. ret = i915_wait_request(obj->last_read_req[i]);
  1247. if (ret)
  1248. return ret;
  1249. i915_gem_object_retire__read(obj, i);
  1250. }
  1251. RQ_BUG_ON(obj->active);
  1252. }
  1253. return 0;
  1254. }
  1255. static void
  1256. i915_gem_object_retire_request(struct drm_i915_gem_object *obj,
  1257. struct drm_i915_gem_request *req)
  1258. {
  1259. int ring = req->ring->id;
  1260. if (obj->last_read_req[ring] == req)
  1261. i915_gem_object_retire__read(obj, ring);
  1262. else if (obj->last_write_req == req)
  1263. i915_gem_object_retire__write(obj);
  1264. __i915_gem_request_retire__upto(req);
  1265. }
  1266. /* A nonblocking variant of the above wait. This is a highly dangerous routine
  1267. * as the object state may change during this call.
  1268. */
  1269. static __must_check int
  1270. i915_gem_object_wait_rendering__nonblocking(struct drm_i915_gem_object *obj,
  1271. struct intel_rps_client *rps,
  1272. bool readonly)
  1273. {
  1274. struct drm_device *dev = obj->base.dev;
  1275. struct drm_i915_private *dev_priv = dev->dev_private;
  1276. struct drm_i915_gem_request *requests[I915_NUM_RINGS];
  1277. unsigned reset_counter;
  1278. int ret, i, n = 0;
  1279. BUG_ON(!mutex_is_locked(&dev->struct_mutex));
  1280. BUG_ON(!dev_priv->mm.interruptible);
  1281. if (!obj->active)
  1282. return 0;
  1283. ret = i915_gem_check_wedge(&dev_priv->gpu_error, true);
  1284. if (ret)
  1285. return ret;
  1286. reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
  1287. if (readonly) {
  1288. struct drm_i915_gem_request *req;
  1289. req = obj->last_write_req;
  1290. if (req == NULL)
  1291. return 0;
  1292. requests[n++] = i915_gem_request_reference(req);
  1293. } else {
  1294. for (i = 0; i < I915_NUM_RINGS; i++) {
  1295. struct drm_i915_gem_request *req;
  1296. req = obj->last_read_req[i];
  1297. if (req == NULL)
  1298. continue;
  1299. requests[n++] = i915_gem_request_reference(req);
  1300. }
  1301. }
  1302. mutex_unlock(&dev->struct_mutex);
  1303. for (i = 0; ret == 0 && i < n; i++)
  1304. ret = __i915_wait_request(requests[i], reset_counter, true,
  1305. NULL, rps);
  1306. mutex_lock(&dev->struct_mutex);
  1307. for (i = 0; i < n; i++) {
  1308. if (ret == 0)
  1309. i915_gem_object_retire_request(obj, requests[i]);
  1310. i915_gem_request_unreference(requests[i]);
  1311. }
  1312. return ret;
  1313. }
  1314. static struct intel_rps_client *to_rps_client(struct drm_file *file)
  1315. {
  1316. struct drm_i915_file_private *fpriv = file->driver_priv;
  1317. return &fpriv->rps;
  1318. }
  1319. /**
  1320. * Called when user space prepares to use an object with the CPU, either
  1321. * through the mmap ioctl's mapping or a GTT mapping.
  1322. */
  1323. int
  1324. i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
  1325. struct drm_file *file)
  1326. {
  1327. struct drm_i915_gem_set_domain *args = data;
  1328. struct drm_i915_gem_object *obj;
  1329. uint32_t read_domains = args->read_domains;
  1330. uint32_t write_domain = args->write_domain;
  1331. int ret;
  1332. /* Only handle setting domains to types used by the CPU. */
  1333. if (write_domain & I915_GEM_GPU_DOMAINS)
  1334. return -EINVAL;
  1335. if (read_domains & I915_GEM_GPU_DOMAINS)
  1336. return -EINVAL;
  1337. /* Having something in the write domain implies it's in the read
  1338. * domain, and only that read domain. Enforce that in the request.
  1339. */
  1340. if (write_domain != 0 && read_domains != write_domain)
  1341. return -EINVAL;
  1342. ret = i915_mutex_lock_interruptible(dev);
  1343. if (ret)
  1344. return ret;
  1345. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  1346. if (&obj->base == NULL) {
  1347. ret = -ENOENT;
  1348. goto unlock;
  1349. }
  1350. /* Try to flush the object off the GPU without holding the lock.
  1351. * We will repeat the flush holding the lock in the normal manner
  1352. * to catch cases where we are gazumped.
  1353. */
  1354. ret = i915_gem_object_wait_rendering__nonblocking(obj,
  1355. to_rps_client(file),
  1356. !write_domain);
  1357. if (ret)
  1358. goto unref;
  1359. if (read_domains & I915_GEM_DOMAIN_GTT)
  1360. ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
  1361. else
  1362. ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
  1363. if (write_domain != 0)
  1364. intel_fb_obj_invalidate(obj,
  1365. write_domain == I915_GEM_DOMAIN_GTT ?
  1366. ORIGIN_GTT : ORIGIN_CPU);
  1367. unref:
  1368. drm_gem_object_unreference(&obj->base);
  1369. unlock:
  1370. mutex_unlock(&dev->struct_mutex);
  1371. return ret;
  1372. }
  1373. /**
  1374. * Called when user space has done writes to this buffer
  1375. */
  1376. int
  1377. i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
  1378. struct drm_file *file)
  1379. {
  1380. struct drm_i915_gem_sw_finish *args = data;
  1381. struct drm_i915_gem_object *obj;
  1382. int ret = 0;
  1383. ret = i915_mutex_lock_interruptible(dev);
  1384. if (ret)
  1385. return ret;
  1386. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  1387. if (&obj->base == NULL) {
  1388. ret = -ENOENT;
  1389. goto unlock;
  1390. }
  1391. /* Pinned buffers may be scanout, so flush the cache */
  1392. if (obj->pin_display)
  1393. i915_gem_object_flush_cpu_write_domain(obj);
  1394. drm_gem_object_unreference(&obj->base);
  1395. unlock:
  1396. mutex_unlock(&dev->struct_mutex);
  1397. return ret;
  1398. }
  1399. /**
  1400. * Maps the contents of an object, returning the address it is mapped
  1401. * into.
  1402. *
  1403. * While the mapping holds a reference on the contents of the object, it doesn't
  1404. * imply a ref on the object itself.
  1405. *
  1406. * IMPORTANT:
  1407. *
  1408. * DRM driver writers who look a this function as an example for how to do GEM
  1409. * mmap support, please don't implement mmap support like here. The modern way
  1410. * to implement DRM mmap support is with an mmap offset ioctl (like
  1411. * i915_gem_mmap_gtt) and then using the mmap syscall on the DRM fd directly.
  1412. * That way debug tooling like valgrind will understand what's going on, hiding
  1413. * the mmap call in a driver private ioctl will break that. The i915 driver only
  1414. * does cpu mmaps this way because we didn't know better.
  1415. */
  1416. int
  1417. i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
  1418. struct drm_file *file)
  1419. {
  1420. struct drm_i915_gem_mmap *args = data;
  1421. struct drm_gem_object *obj;
  1422. unsigned long addr;
  1423. if (args->flags & ~(I915_MMAP_WC))
  1424. return -EINVAL;
  1425. if (args->flags & I915_MMAP_WC && !cpu_has_pat)
  1426. return -ENODEV;
  1427. obj = drm_gem_object_lookup(dev, file, args->handle);
  1428. if (obj == NULL)
  1429. return -ENOENT;
  1430. /* prime objects have no backing filp to GEM mmap
  1431. * pages from.
  1432. */
  1433. if (!obj->filp) {
  1434. drm_gem_object_unreference_unlocked(obj);
  1435. return -EINVAL;
  1436. }
  1437. addr = vm_mmap(obj->filp, 0, args->size,
  1438. PROT_READ | PROT_WRITE, MAP_SHARED,
  1439. args->offset);
  1440. if (args->flags & I915_MMAP_WC) {
  1441. struct mm_struct *mm = current->mm;
  1442. struct vm_area_struct *vma;
  1443. down_write(&mm->mmap_sem);
  1444. vma = find_vma(mm, addr);
  1445. if (vma)
  1446. vma->vm_page_prot =
  1447. pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
  1448. else
  1449. addr = -ENOMEM;
  1450. up_write(&mm->mmap_sem);
  1451. }
  1452. drm_gem_object_unreference_unlocked(obj);
  1453. if (IS_ERR((void *)addr))
  1454. return addr;
  1455. args->addr_ptr = (uint64_t) addr;
  1456. return 0;
  1457. }
  1458. /**
  1459. * i915_gem_fault - fault a page into the GTT
  1460. * @vma: VMA in question
  1461. * @vmf: fault info
  1462. *
  1463. * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
  1464. * from userspace. The fault handler takes care of binding the object to
  1465. * the GTT (if needed), allocating and programming a fence register (again,
  1466. * only if needed based on whether the old reg is still valid or the object
  1467. * is tiled) and inserting a new PTE into the faulting process.
  1468. *
  1469. * Note that the faulting process may involve evicting existing objects
  1470. * from the GTT and/or fence registers to make room. So performance may
  1471. * suffer if the GTT working set is large or there are few fence registers
  1472. * left.
  1473. */
  1474. int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  1475. {
  1476. struct drm_i915_gem_object *obj = to_intel_bo(vma->vm_private_data);
  1477. struct drm_device *dev = obj->base.dev;
  1478. struct drm_i915_private *dev_priv = dev->dev_private;
  1479. struct i915_ggtt_view view = i915_ggtt_view_normal;
  1480. pgoff_t page_offset;
  1481. unsigned long pfn;
  1482. int ret = 0;
  1483. bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
  1484. intel_runtime_pm_get(dev_priv);
  1485. /* We don't use vmf->pgoff since that has the fake offset */
  1486. page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
  1487. PAGE_SHIFT;
  1488. ret = i915_mutex_lock_interruptible(dev);
  1489. if (ret)
  1490. goto out;
  1491. trace_i915_gem_object_fault(obj, page_offset, true, write);
  1492. /* Try to flush the object off the GPU first without holding the lock.
  1493. * Upon reacquiring the lock, we will perform our sanity checks and then
  1494. * repeat the flush holding the lock in the normal manner to catch cases
  1495. * where we are gazumped.
  1496. */
  1497. ret = i915_gem_object_wait_rendering__nonblocking(obj, NULL, !write);
  1498. if (ret)
  1499. goto unlock;
  1500. /* Access to snoopable pages through the GTT is incoherent. */
  1501. if (obj->cache_level != I915_CACHE_NONE && !HAS_LLC(dev)) {
  1502. ret = -EFAULT;
  1503. goto unlock;
  1504. }
  1505. /* Use a partial view if the object is bigger than the aperture. */
  1506. if (obj->base.size >= dev_priv->gtt.mappable_end &&
  1507. obj->tiling_mode == I915_TILING_NONE) {
  1508. static const unsigned int chunk_size = 256; // 1 MiB
  1509. memset(&view, 0, sizeof(view));
  1510. view.type = I915_GGTT_VIEW_PARTIAL;
  1511. view.params.partial.offset = rounddown(page_offset, chunk_size);
  1512. view.params.partial.size =
  1513. min_t(unsigned int,
  1514. chunk_size,
  1515. (vma->vm_end - vma->vm_start)/PAGE_SIZE -
  1516. view.params.partial.offset);
  1517. }
  1518. /* Now pin it into the GTT if needed */
  1519. ret = i915_gem_object_ggtt_pin(obj, &view, 0, PIN_MAPPABLE);
  1520. if (ret)
  1521. goto unlock;
  1522. ret = i915_gem_object_set_to_gtt_domain(obj, write);
  1523. if (ret)
  1524. goto unpin;
  1525. ret = i915_gem_object_get_fence(obj);
  1526. if (ret)
  1527. goto unpin;
  1528. /* Finally, remap it using the new GTT offset */
  1529. pfn = dev_priv->gtt.mappable_base +
  1530. i915_gem_obj_ggtt_offset_view(obj, &view);
  1531. pfn >>= PAGE_SHIFT;
  1532. if (unlikely(view.type == I915_GGTT_VIEW_PARTIAL)) {
  1533. /* Overriding existing pages in partial view does not cause
  1534. * us any trouble as TLBs are still valid because the fault
  1535. * is due to userspace losing part of the mapping or never
  1536. * having accessed it before (at this partials' range).
  1537. */
  1538. unsigned long base = vma->vm_start +
  1539. (view.params.partial.offset << PAGE_SHIFT);
  1540. unsigned int i;
  1541. for (i = 0; i < view.params.partial.size; i++) {
  1542. ret = vm_insert_pfn(vma, base + i * PAGE_SIZE, pfn + i);
  1543. if (ret)
  1544. break;
  1545. }
  1546. obj->fault_mappable = true;
  1547. } else {
  1548. if (!obj->fault_mappable) {
  1549. unsigned long size = min_t(unsigned long,
  1550. vma->vm_end - vma->vm_start,
  1551. obj->base.size);
  1552. int i;
  1553. for (i = 0; i < size >> PAGE_SHIFT; i++) {
  1554. ret = vm_insert_pfn(vma,
  1555. (unsigned long)vma->vm_start + i * PAGE_SIZE,
  1556. pfn + i);
  1557. if (ret)
  1558. break;
  1559. }
  1560. obj->fault_mappable = true;
  1561. } else
  1562. ret = vm_insert_pfn(vma,
  1563. (unsigned long)vmf->virtual_address,
  1564. pfn + page_offset);
  1565. }
  1566. unpin:
  1567. i915_gem_object_ggtt_unpin_view(obj, &view);
  1568. unlock:
  1569. mutex_unlock(&dev->struct_mutex);
  1570. out:
  1571. switch (ret) {
  1572. case -EIO:
  1573. /*
  1574. * We eat errors when the gpu is terminally wedged to avoid
  1575. * userspace unduly crashing (gl has no provisions for mmaps to
  1576. * fail). But any other -EIO isn't ours (e.g. swap in failure)
  1577. * and so needs to be reported.
  1578. */
  1579. if (!i915_terminally_wedged(&dev_priv->gpu_error)) {
  1580. ret = VM_FAULT_SIGBUS;
  1581. break;
  1582. }
  1583. case -EAGAIN:
  1584. /*
  1585. * EAGAIN means the gpu is hung and we'll wait for the error
  1586. * handler to reset everything when re-faulting in
  1587. * i915_mutex_lock_interruptible.
  1588. */
  1589. case 0:
  1590. case -ERESTARTSYS:
  1591. case -EINTR:
  1592. case -EBUSY:
  1593. /*
  1594. * EBUSY is ok: this just means that another thread
  1595. * already did the job.
  1596. */
  1597. ret = VM_FAULT_NOPAGE;
  1598. break;
  1599. case -ENOMEM:
  1600. ret = VM_FAULT_OOM;
  1601. break;
  1602. case -ENOSPC:
  1603. case -EFAULT:
  1604. ret = VM_FAULT_SIGBUS;
  1605. break;
  1606. default:
  1607. WARN_ONCE(ret, "unhandled error in i915_gem_fault: %i\n", ret);
  1608. ret = VM_FAULT_SIGBUS;
  1609. break;
  1610. }
  1611. intel_runtime_pm_put(dev_priv);
  1612. return ret;
  1613. }
  1614. /**
  1615. * i915_gem_release_mmap - remove physical page mappings
  1616. * @obj: obj in question
  1617. *
  1618. * Preserve the reservation of the mmapping with the DRM core code, but
  1619. * relinquish ownership of the pages back to the system.
  1620. *
  1621. * It is vital that we remove the page mapping if we have mapped a tiled
  1622. * object through the GTT and then lose the fence register due to
  1623. * resource pressure. Similarly if the object has been moved out of the
  1624. * aperture, than pages mapped into userspace must be revoked. Removing the
  1625. * mapping will then trigger a page fault on the next user access, allowing
  1626. * fixup by i915_gem_fault().
  1627. */
  1628. void
  1629. i915_gem_release_mmap(struct drm_i915_gem_object *obj)
  1630. {
  1631. if (!obj->fault_mappable)
  1632. return;
  1633. drm_vma_node_unmap(&obj->base.vma_node,
  1634. obj->base.dev->anon_inode->i_mapping);
  1635. obj->fault_mappable = false;
  1636. }
  1637. void
  1638. i915_gem_release_all_mmaps(struct drm_i915_private *dev_priv)
  1639. {
  1640. struct drm_i915_gem_object *obj;
  1641. list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list)
  1642. i915_gem_release_mmap(obj);
  1643. }
  1644. uint32_t
  1645. i915_gem_get_gtt_size(struct drm_device *dev, uint32_t size, int tiling_mode)
  1646. {
  1647. uint32_t gtt_size;
  1648. if (INTEL_INFO(dev)->gen >= 4 ||
  1649. tiling_mode == I915_TILING_NONE)
  1650. return size;
  1651. /* Previous chips need a power-of-two fence region when tiling */
  1652. if (INTEL_INFO(dev)->gen == 3)
  1653. gtt_size = 1024*1024;
  1654. else
  1655. gtt_size = 512*1024;
  1656. while (gtt_size < size)
  1657. gtt_size <<= 1;
  1658. return gtt_size;
  1659. }
  1660. /**
  1661. * i915_gem_get_gtt_alignment - return required GTT alignment for an object
  1662. * @obj: object to check
  1663. *
  1664. * Return the required GTT alignment for an object, taking into account
  1665. * potential fence register mapping.
  1666. */
  1667. uint32_t
  1668. i915_gem_get_gtt_alignment(struct drm_device *dev, uint32_t size,
  1669. int tiling_mode, bool fenced)
  1670. {
  1671. /*
  1672. * Minimum alignment is 4k (GTT page size), but might be greater
  1673. * if a fence register is needed for the object.
  1674. */
  1675. if (INTEL_INFO(dev)->gen >= 4 || (!fenced && IS_G33(dev)) ||
  1676. tiling_mode == I915_TILING_NONE)
  1677. return 4096;
  1678. /*
  1679. * Previous chips need to be aligned to the size of the smallest
  1680. * fence register that can contain the object.
  1681. */
  1682. return i915_gem_get_gtt_size(dev, size, tiling_mode);
  1683. }
  1684. static int i915_gem_object_create_mmap_offset(struct drm_i915_gem_object *obj)
  1685. {
  1686. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  1687. int ret;
  1688. if (drm_vma_node_has_offset(&obj->base.vma_node))
  1689. return 0;
  1690. dev_priv->mm.shrinker_no_lock_stealing = true;
  1691. ret = drm_gem_create_mmap_offset(&obj->base);
  1692. if (ret != -ENOSPC)
  1693. goto out;
  1694. /* Badly fragmented mmap space? The only way we can recover
  1695. * space is by destroying unwanted objects. We can't randomly release
  1696. * mmap_offsets as userspace expects them to be persistent for the
  1697. * lifetime of the objects. The closest we can is to release the
  1698. * offsets on purgeable objects by truncating it and marking it purged,
  1699. * which prevents userspace from ever using that object again.
  1700. */
  1701. i915_gem_shrink(dev_priv,
  1702. obj->base.size >> PAGE_SHIFT,
  1703. I915_SHRINK_BOUND |
  1704. I915_SHRINK_UNBOUND |
  1705. I915_SHRINK_PURGEABLE);
  1706. ret = drm_gem_create_mmap_offset(&obj->base);
  1707. if (ret != -ENOSPC)
  1708. goto out;
  1709. i915_gem_shrink_all(dev_priv);
  1710. ret = drm_gem_create_mmap_offset(&obj->base);
  1711. out:
  1712. dev_priv->mm.shrinker_no_lock_stealing = false;
  1713. return ret;
  1714. }
  1715. static void i915_gem_object_free_mmap_offset(struct drm_i915_gem_object *obj)
  1716. {
  1717. drm_gem_free_mmap_offset(&obj->base);
  1718. }
  1719. int
  1720. i915_gem_mmap_gtt(struct drm_file *file,
  1721. struct drm_device *dev,
  1722. uint32_t handle,
  1723. uint64_t *offset)
  1724. {
  1725. struct drm_i915_gem_object *obj;
  1726. int ret;
  1727. ret = i915_mutex_lock_interruptible(dev);
  1728. if (ret)
  1729. return ret;
  1730. obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
  1731. if (&obj->base == NULL) {
  1732. ret = -ENOENT;
  1733. goto unlock;
  1734. }
  1735. if (obj->madv != I915_MADV_WILLNEED) {
  1736. DRM_DEBUG("Attempting to mmap a purgeable buffer\n");
  1737. ret = -EFAULT;
  1738. goto out;
  1739. }
  1740. ret = i915_gem_object_create_mmap_offset(obj);
  1741. if (ret)
  1742. goto out;
  1743. *offset = drm_vma_node_offset_addr(&obj->base.vma_node);
  1744. out:
  1745. drm_gem_object_unreference(&obj->base);
  1746. unlock:
  1747. mutex_unlock(&dev->struct_mutex);
  1748. return ret;
  1749. }
  1750. /**
  1751. * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
  1752. * @dev: DRM device
  1753. * @data: GTT mapping ioctl data
  1754. * @file: GEM object info
  1755. *
  1756. * Simply returns the fake offset to userspace so it can mmap it.
  1757. * The mmap call will end up in drm_gem_mmap(), which will set things
  1758. * up so we can get faults in the handler above.
  1759. *
  1760. * The fault handler will take care of binding the object into the GTT
  1761. * (since it may have been evicted to make room for something), allocating
  1762. * a fence register, and mapping the appropriate aperture address into
  1763. * userspace.
  1764. */
  1765. int
  1766. i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
  1767. struct drm_file *file)
  1768. {
  1769. struct drm_i915_gem_mmap_gtt *args = data;
  1770. return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset);
  1771. }
  1772. /* Immediately discard the backing storage */
  1773. static void
  1774. i915_gem_object_truncate(struct drm_i915_gem_object *obj)
  1775. {
  1776. i915_gem_object_free_mmap_offset(obj);
  1777. if (obj->base.filp == NULL)
  1778. return;
  1779. /* Our goal here is to return as much of the memory as
  1780. * is possible back to the system as we are called from OOM.
  1781. * To do this we must instruct the shmfs to drop all of its
  1782. * backing pages, *now*.
  1783. */
  1784. shmem_truncate_range(file_inode(obj->base.filp), 0, (loff_t)-1);
  1785. obj->madv = __I915_MADV_PURGED;
  1786. }
  1787. /* Try to discard unwanted pages */
  1788. static void
  1789. i915_gem_object_invalidate(struct drm_i915_gem_object *obj)
  1790. {
  1791. struct address_space *mapping;
  1792. switch (obj->madv) {
  1793. case I915_MADV_DONTNEED:
  1794. i915_gem_object_truncate(obj);
  1795. case __I915_MADV_PURGED:
  1796. return;
  1797. }
  1798. if (obj->base.filp == NULL)
  1799. return;
  1800. mapping = file_inode(obj->base.filp)->i_mapping,
  1801. invalidate_mapping_pages(mapping, 0, (loff_t)-1);
  1802. }
  1803. static void
  1804. i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj)
  1805. {
  1806. struct sg_page_iter sg_iter;
  1807. int ret;
  1808. BUG_ON(obj->madv == __I915_MADV_PURGED);
  1809. ret = i915_gem_object_set_to_cpu_domain(obj, true);
  1810. if (ret) {
  1811. /* In the event of a disaster, abandon all caches and
  1812. * hope for the best.
  1813. */
  1814. WARN_ON(ret != -EIO);
  1815. i915_gem_clflush_object(obj, true);
  1816. obj->base.read_domains = obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  1817. }
  1818. i915_gem_gtt_finish_object(obj);
  1819. if (i915_gem_object_needs_bit17_swizzle(obj))
  1820. i915_gem_object_save_bit_17_swizzle(obj);
  1821. if (obj->madv == I915_MADV_DONTNEED)
  1822. obj->dirty = 0;
  1823. for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents, 0) {
  1824. struct page *page = sg_page_iter_page(&sg_iter);
  1825. if (obj->dirty)
  1826. set_page_dirty(page);
  1827. if (obj->madv == I915_MADV_WILLNEED)
  1828. mark_page_accessed(page);
  1829. page_cache_release(page);
  1830. }
  1831. obj->dirty = 0;
  1832. sg_free_table(obj->pages);
  1833. kfree(obj->pages);
  1834. }
  1835. int
  1836. i915_gem_object_put_pages(struct drm_i915_gem_object *obj)
  1837. {
  1838. const struct drm_i915_gem_object_ops *ops = obj->ops;
  1839. if (obj->pages == NULL)
  1840. return 0;
  1841. if (obj->pages_pin_count)
  1842. return -EBUSY;
  1843. BUG_ON(i915_gem_obj_bound_any(obj));
  1844. /* ->put_pages might need to allocate memory for the bit17 swizzle
  1845. * array, hence protect them from being reaped by removing them from gtt
  1846. * lists early. */
  1847. list_del(&obj->global_list);
  1848. ops->put_pages(obj);
  1849. obj->pages = NULL;
  1850. i915_gem_object_invalidate(obj);
  1851. return 0;
  1852. }
  1853. static int
  1854. i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj)
  1855. {
  1856. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  1857. int page_count, i;
  1858. struct address_space *mapping;
  1859. struct sg_table *st;
  1860. struct scatterlist *sg;
  1861. struct sg_page_iter sg_iter;
  1862. struct page *page;
  1863. unsigned long last_pfn = 0; /* suppress gcc warning */
  1864. int ret;
  1865. gfp_t gfp;
  1866. /* Assert that the object is not currently in any GPU domain. As it
  1867. * wasn't in the GTT, there shouldn't be any way it could have been in
  1868. * a GPU cache
  1869. */
  1870. BUG_ON(obj->base.read_domains & I915_GEM_GPU_DOMAINS);
  1871. BUG_ON(obj->base.write_domain & I915_GEM_GPU_DOMAINS);
  1872. st = kmalloc(sizeof(*st), GFP_KERNEL);
  1873. if (st == NULL)
  1874. return -ENOMEM;
  1875. page_count = obj->base.size / PAGE_SIZE;
  1876. if (sg_alloc_table(st, page_count, GFP_KERNEL)) {
  1877. kfree(st);
  1878. return -ENOMEM;
  1879. }
  1880. /* Get the list of pages out of our struct file. They'll be pinned
  1881. * at this point until we release them.
  1882. *
  1883. * Fail silently without starting the shrinker
  1884. */
  1885. mapping = file_inode(obj->base.filp)->i_mapping;
  1886. gfp = mapping_gfp_constraint(mapping, ~(__GFP_IO | __GFP_RECLAIM));
  1887. gfp |= __GFP_NORETRY | __GFP_NOWARN;
  1888. sg = st->sgl;
  1889. st->nents = 0;
  1890. for (i = 0; i < page_count; i++) {
  1891. page = shmem_read_mapping_page_gfp(mapping, i, gfp);
  1892. if (IS_ERR(page)) {
  1893. i915_gem_shrink(dev_priv,
  1894. page_count,
  1895. I915_SHRINK_BOUND |
  1896. I915_SHRINK_UNBOUND |
  1897. I915_SHRINK_PURGEABLE);
  1898. page = shmem_read_mapping_page_gfp(mapping, i, gfp);
  1899. }
  1900. if (IS_ERR(page)) {
  1901. /* We've tried hard to allocate the memory by reaping
  1902. * our own buffer, now let the real VM do its job and
  1903. * go down in flames if truly OOM.
  1904. */
  1905. i915_gem_shrink_all(dev_priv);
  1906. page = shmem_read_mapping_page(mapping, i);
  1907. if (IS_ERR(page)) {
  1908. ret = PTR_ERR(page);
  1909. goto err_pages;
  1910. }
  1911. }
  1912. #ifdef CONFIG_SWIOTLB
  1913. if (swiotlb_nr_tbl()) {
  1914. st->nents++;
  1915. sg_set_page(sg, page, PAGE_SIZE, 0);
  1916. sg = sg_next(sg);
  1917. continue;
  1918. }
  1919. #endif
  1920. if (!i || page_to_pfn(page) != last_pfn + 1) {
  1921. if (i)
  1922. sg = sg_next(sg);
  1923. st->nents++;
  1924. sg_set_page(sg, page, PAGE_SIZE, 0);
  1925. } else {
  1926. sg->length += PAGE_SIZE;
  1927. }
  1928. last_pfn = page_to_pfn(page);
  1929. /* Check that the i965g/gm workaround works. */
  1930. WARN_ON((gfp & __GFP_DMA32) && (last_pfn >= 0x00100000UL));
  1931. }
  1932. #ifdef CONFIG_SWIOTLB
  1933. if (!swiotlb_nr_tbl())
  1934. #endif
  1935. sg_mark_end(sg);
  1936. obj->pages = st;
  1937. ret = i915_gem_gtt_prepare_object(obj);
  1938. if (ret)
  1939. goto err_pages;
  1940. if (i915_gem_object_needs_bit17_swizzle(obj))
  1941. i915_gem_object_do_bit_17_swizzle(obj);
  1942. if (obj->tiling_mode != I915_TILING_NONE &&
  1943. dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES)
  1944. i915_gem_object_pin_pages(obj);
  1945. return 0;
  1946. err_pages:
  1947. sg_mark_end(sg);
  1948. for_each_sg_page(st->sgl, &sg_iter, st->nents, 0)
  1949. page_cache_release(sg_page_iter_page(&sg_iter));
  1950. sg_free_table(st);
  1951. kfree(st);
  1952. /* shmemfs first checks if there is enough memory to allocate the page
  1953. * and reports ENOSPC should there be insufficient, along with the usual
  1954. * ENOMEM for a genuine allocation failure.
  1955. *
  1956. * We use ENOSPC in our driver to mean that we have run out of aperture
  1957. * space and so want to translate the error from shmemfs back to our
  1958. * usual understanding of ENOMEM.
  1959. */
  1960. if (ret == -ENOSPC)
  1961. ret = -ENOMEM;
  1962. return ret;
  1963. }
  1964. /* Ensure that the associated pages are gathered from the backing storage
  1965. * and pinned into our object. i915_gem_object_get_pages() may be called
  1966. * multiple times before they are released by a single call to
  1967. * i915_gem_object_put_pages() - once the pages are no longer referenced
  1968. * either as a result of memory pressure (reaping pages under the shrinker)
  1969. * or as the object is itself released.
  1970. */
  1971. int
  1972. i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
  1973. {
  1974. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  1975. const struct drm_i915_gem_object_ops *ops = obj->ops;
  1976. int ret;
  1977. if (obj->pages)
  1978. return 0;
  1979. if (obj->madv != I915_MADV_WILLNEED) {
  1980. DRM_DEBUG("Attempting to obtain a purgeable object\n");
  1981. return -EFAULT;
  1982. }
  1983. BUG_ON(obj->pages_pin_count);
  1984. ret = ops->get_pages(obj);
  1985. if (ret)
  1986. return ret;
  1987. list_add_tail(&obj->global_list, &dev_priv->mm.unbound_list);
  1988. obj->get_page.sg = obj->pages->sgl;
  1989. obj->get_page.last = 0;
  1990. return 0;
  1991. }
  1992. void i915_vma_move_to_active(struct i915_vma *vma,
  1993. struct drm_i915_gem_request *req)
  1994. {
  1995. struct drm_i915_gem_object *obj = vma->obj;
  1996. struct intel_engine_cs *ring;
  1997. ring = i915_gem_request_get_ring(req);
  1998. /* Add a reference if we're newly entering the active list. */
  1999. if (obj->active == 0)
  2000. drm_gem_object_reference(&obj->base);
  2001. obj->active |= intel_ring_flag(ring);
  2002. list_move_tail(&obj->ring_list[ring->id], &ring->active_list);
  2003. i915_gem_request_assign(&obj->last_read_req[ring->id], req);
  2004. list_move_tail(&vma->mm_list, &vma->vm->active_list);
  2005. }
  2006. static void
  2007. i915_gem_object_retire__write(struct drm_i915_gem_object *obj)
  2008. {
  2009. RQ_BUG_ON(obj->last_write_req == NULL);
  2010. RQ_BUG_ON(!(obj->active & intel_ring_flag(obj->last_write_req->ring)));
  2011. i915_gem_request_assign(&obj->last_write_req, NULL);
  2012. intel_fb_obj_flush(obj, true, ORIGIN_CS);
  2013. }
  2014. static void
  2015. i915_gem_object_retire__read(struct drm_i915_gem_object *obj, int ring)
  2016. {
  2017. struct i915_vma *vma;
  2018. RQ_BUG_ON(obj->last_read_req[ring] == NULL);
  2019. RQ_BUG_ON(!(obj->active & (1 << ring)));
  2020. list_del_init(&obj->ring_list[ring]);
  2021. i915_gem_request_assign(&obj->last_read_req[ring], NULL);
  2022. if (obj->last_write_req && obj->last_write_req->ring->id == ring)
  2023. i915_gem_object_retire__write(obj);
  2024. obj->active &= ~(1 << ring);
  2025. if (obj->active)
  2026. return;
  2027. /* Bump our place on the bound list to keep it roughly in LRU order
  2028. * so that we don't steal from recently used but inactive objects
  2029. * (unless we are forced to ofc!)
  2030. */
  2031. list_move_tail(&obj->global_list,
  2032. &to_i915(obj->base.dev)->mm.bound_list);
  2033. list_for_each_entry(vma, &obj->vma_list, vma_link) {
  2034. if (!list_empty(&vma->mm_list))
  2035. list_move_tail(&vma->mm_list, &vma->vm->inactive_list);
  2036. }
  2037. i915_gem_request_assign(&obj->last_fenced_req, NULL);
  2038. drm_gem_object_unreference(&obj->base);
  2039. }
  2040. static int
  2041. i915_gem_init_seqno(struct drm_device *dev, u32 seqno)
  2042. {
  2043. struct drm_i915_private *dev_priv = dev->dev_private;
  2044. struct intel_engine_cs *ring;
  2045. int ret, i, j;
  2046. /* Carefully retire all requests without writing to the rings */
  2047. for_each_ring(ring, dev_priv, i) {
  2048. ret = intel_ring_idle(ring);
  2049. if (ret)
  2050. return ret;
  2051. }
  2052. i915_gem_retire_requests(dev);
  2053. /* Finally reset hw state */
  2054. for_each_ring(ring, dev_priv, i) {
  2055. intel_ring_init_seqno(ring, seqno);
  2056. for (j = 0; j < ARRAY_SIZE(ring->semaphore.sync_seqno); j++)
  2057. ring->semaphore.sync_seqno[j] = 0;
  2058. }
  2059. return 0;
  2060. }
  2061. int i915_gem_set_seqno(struct drm_device *dev, u32 seqno)
  2062. {
  2063. struct drm_i915_private *dev_priv = dev->dev_private;
  2064. int ret;
  2065. if (seqno == 0)
  2066. return -EINVAL;
  2067. /* HWS page needs to be set less than what we
  2068. * will inject to ring
  2069. */
  2070. ret = i915_gem_init_seqno(dev, seqno - 1);
  2071. if (ret)
  2072. return ret;
  2073. /* Carefully set the last_seqno value so that wrap
  2074. * detection still works
  2075. */
  2076. dev_priv->next_seqno = seqno;
  2077. dev_priv->last_seqno = seqno - 1;
  2078. if (dev_priv->last_seqno == 0)
  2079. dev_priv->last_seqno--;
  2080. return 0;
  2081. }
  2082. int
  2083. i915_gem_get_seqno(struct drm_device *dev, u32 *seqno)
  2084. {
  2085. struct drm_i915_private *dev_priv = dev->dev_private;
  2086. /* reserve 0 for non-seqno */
  2087. if (dev_priv->next_seqno == 0) {
  2088. int ret = i915_gem_init_seqno(dev, 0);
  2089. if (ret)
  2090. return ret;
  2091. dev_priv->next_seqno = 1;
  2092. }
  2093. *seqno = dev_priv->last_seqno = dev_priv->next_seqno++;
  2094. return 0;
  2095. }
  2096. /*
  2097. * NB: This function is not allowed to fail. Doing so would mean the the
  2098. * request is not being tracked for completion but the work itself is
  2099. * going to happen on the hardware. This would be a Bad Thing(tm).
  2100. */
  2101. void __i915_add_request(struct drm_i915_gem_request *request,
  2102. struct drm_i915_gem_object *obj,
  2103. bool flush_caches)
  2104. {
  2105. struct intel_engine_cs *ring;
  2106. struct drm_i915_private *dev_priv;
  2107. struct intel_ringbuffer *ringbuf;
  2108. u32 request_start;
  2109. int ret;
  2110. if (WARN_ON(request == NULL))
  2111. return;
  2112. ring = request->ring;
  2113. dev_priv = ring->dev->dev_private;
  2114. ringbuf = request->ringbuf;
  2115. /*
  2116. * To ensure that this call will not fail, space for its emissions
  2117. * should already have been reserved in the ring buffer. Let the ring
  2118. * know that it is time to use that space up.
  2119. */
  2120. intel_ring_reserved_space_use(ringbuf);
  2121. request_start = intel_ring_get_tail(ringbuf);
  2122. /*
  2123. * Emit any outstanding flushes - execbuf can fail to emit the flush
  2124. * after having emitted the batchbuffer command. Hence we need to fix
  2125. * things up similar to emitting the lazy request. The difference here
  2126. * is that the flush _must_ happen before the next request, no matter
  2127. * what.
  2128. */
  2129. if (flush_caches) {
  2130. if (i915.enable_execlists)
  2131. ret = logical_ring_flush_all_caches(request);
  2132. else
  2133. ret = intel_ring_flush_all_caches(request);
  2134. /* Not allowed to fail! */
  2135. WARN(ret, "*_ring_flush_all_caches failed: %d!\n", ret);
  2136. }
  2137. /* Record the position of the start of the request so that
  2138. * should we detect the updated seqno part-way through the
  2139. * GPU processing the request, we never over-estimate the
  2140. * position of the head.
  2141. */
  2142. request->postfix = intel_ring_get_tail(ringbuf);
  2143. if (i915.enable_execlists)
  2144. ret = ring->emit_request(request);
  2145. else {
  2146. ret = ring->add_request(request);
  2147. request->tail = intel_ring_get_tail(ringbuf);
  2148. }
  2149. /* Not allowed to fail! */
  2150. WARN(ret, "emit|add_request failed: %d!\n", ret);
  2151. request->head = request_start;
  2152. /* Whilst this request exists, batch_obj will be on the
  2153. * active_list, and so will hold the active reference. Only when this
  2154. * request is retired will the the batch_obj be moved onto the
  2155. * inactive_list and lose its active reference. Hence we do not need
  2156. * to explicitly hold another reference here.
  2157. */
  2158. request->batch_obj = obj;
  2159. request->emitted_jiffies = jiffies;
  2160. request->previous_seqno = ring->last_submitted_seqno;
  2161. ring->last_submitted_seqno = request->seqno;
  2162. list_add_tail(&request->list, &ring->request_list);
  2163. trace_i915_gem_request_add(request);
  2164. i915_queue_hangcheck(ring->dev);
  2165. queue_delayed_work(dev_priv->wq,
  2166. &dev_priv->mm.retire_work,
  2167. round_jiffies_up_relative(HZ));
  2168. intel_mark_busy(dev_priv->dev);
  2169. /* Sanity check that the reserved size was large enough. */
  2170. intel_ring_reserved_space_end(ringbuf);
  2171. }
  2172. static bool i915_context_is_banned(struct drm_i915_private *dev_priv,
  2173. const struct intel_context *ctx)
  2174. {
  2175. unsigned long elapsed;
  2176. elapsed = get_seconds() - ctx->hang_stats.guilty_ts;
  2177. if (ctx->hang_stats.banned)
  2178. return true;
  2179. if (ctx->hang_stats.ban_period_seconds &&
  2180. elapsed <= ctx->hang_stats.ban_period_seconds) {
  2181. if (!i915_gem_context_is_default(ctx)) {
  2182. DRM_DEBUG("context hanging too fast, banning!\n");
  2183. return true;
  2184. } else if (i915_stop_ring_allow_ban(dev_priv)) {
  2185. if (i915_stop_ring_allow_warn(dev_priv))
  2186. DRM_ERROR("gpu hanging too fast, banning!\n");
  2187. return true;
  2188. }
  2189. }
  2190. return false;
  2191. }
  2192. static void i915_set_reset_status(struct drm_i915_private *dev_priv,
  2193. struct intel_context *ctx,
  2194. const bool guilty)
  2195. {
  2196. struct i915_ctx_hang_stats *hs;
  2197. if (WARN_ON(!ctx))
  2198. return;
  2199. hs = &ctx->hang_stats;
  2200. if (guilty) {
  2201. hs->banned = i915_context_is_banned(dev_priv, ctx);
  2202. hs->batch_active++;
  2203. hs->guilty_ts = get_seconds();
  2204. } else {
  2205. hs->batch_pending++;
  2206. }
  2207. }
  2208. void i915_gem_request_free(struct kref *req_ref)
  2209. {
  2210. struct drm_i915_gem_request *req = container_of(req_ref,
  2211. typeof(*req), ref);
  2212. struct intel_context *ctx = req->ctx;
  2213. if (req->file_priv)
  2214. i915_gem_request_remove_from_client(req);
  2215. if (ctx) {
  2216. if (i915.enable_execlists) {
  2217. if (ctx != req->ring->default_context)
  2218. intel_lr_context_unpin(req);
  2219. }
  2220. i915_gem_context_unreference(ctx);
  2221. }
  2222. kmem_cache_free(req->i915->requests, req);
  2223. }
  2224. int i915_gem_request_alloc(struct intel_engine_cs *ring,
  2225. struct intel_context *ctx,
  2226. struct drm_i915_gem_request **req_out)
  2227. {
  2228. struct drm_i915_private *dev_priv = to_i915(ring->dev);
  2229. struct drm_i915_gem_request *req;
  2230. int ret;
  2231. if (!req_out)
  2232. return -EINVAL;
  2233. *req_out = NULL;
  2234. req = kmem_cache_zalloc(dev_priv->requests, GFP_KERNEL);
  2235. if (req == NULL)
  2236. return -ENOMEM;
  2237. ret = i915_gem_get_seqno(ring->dev, &req->seqno);
  2238. if (ret)
  2239. goto err;
  2240. kref_init(&req->ref);
  2241. req->i915 = dev_priv;
  2242. req->ring = ring;
  2243. req->ctx = ctx;
  2244. i915_gem_context_reference(req->ctx);
  2245. if (i915.enable_execlists)
  2246. ret = intel_logical_ring_alloc_request_extras(req);
  2247. else
  2248. ret = intel_ring_alloc_request_extras(req);
  2249. if (ret) {
  2250. i915_gem_context_unreference(req->ctx);
  2251. goto err;
  2252. }
  2253. /*
  2254. * Reserve space in the ring buffer for all the commands required to
  2255. * eventually emit this request. This is to guarantee that the
  2256. * i915_add_request() call can't fail. Note that the reserve may need
  2257. * to be redone if the request is not actually submitted straight
  2258. * away, e.g. because a GPU scheduler has deferred it.
  2259. */
  2260. if (i915.enable_execlists)
  2261. ret = intel_logical_ring_reserve_space(req);
  2262. else
  2263. ret = intel_ring_reserve_space(req);
  2264. if (ret) {
  2265. /*
  2266. * At this point, the request is fully allocated even if not
  2267. * fully prepared. Thus it can be cleaned up using the proper
  2268. * free code.
  2269. */
  2270. i915_gem_request_cancel(req);
  2271. return ret;
  2272. }
  2273. *req_out = req;
  2274. return 0;
  2275. err:
  2276. kmem_cache_free(dev_priv->requests, req);
  2277. return ret;
  2278. }
  2279. void i915_gem_request_cancel(struct drm_i915_gem_request *req)
  2280. {
  2281. intel_ring_reserved_space_cancel(req->ringbuf);
  2282. i915_gem_request_unreference(req);
  2283. }
  2284. struct drm_i915_gem_request *
  2285. i915_gem_find_active_request(struct intel_engine_cs *ring)
  2286. {
  2287. struct drm_i915_gem_request *request;
  2288. list_for_each_entry(request, &ring->request_list, list) {
  2289. if (i915_gem_request_completed(request, false))
  2290. continue;
  2291. return request;
  2292. }
  2293. return NULL;
  2294. }
  2295. static void i915_gem_reset_ring_status(struct drm_i915_private *dev_priv,
  2296. struct intel_engine_cs *ring)
  2297. {
  2298. struct drm_i915_gem_request *request;
  2299. bool ring_hung;
  2300. request = i915_gem_find_active_request(ring);
  2301. if (request == NULL)
  2302. return;
  2303. ring_hung = ring->hangcheck.score >= HANGCHECK_SCORE_RING_HUNG;
  2304. i915_set_reset_status(dev_priv, request->ctx, ring_hung);
  2305. list_for_each_entry_continue(request, &ring->request_list, list)
  2306. i915_set_reset_status(dev_priv, request->ctx, false);
  2307. }
  2308. static void i915_gem_reset_ring_cleanup(struct drm_i915_private *dev_priv,
  2309. struct intel_engine_cs *ring)
  2310. {
  2311. while (!list_empty(&ring->active_list)) {
  2312. struct drm_i915_gem_object *obj;
  2313. obj = list_first_entry(&ring->active_list,
  2314. struct drm_i915_gem_object,
  2315. ring_list[ring->id]);
  2316. i915_gem_object_retire__read(obj, ring->id);
  2317. }
  2318. /*
  2319. * Clear the execlists queue up before freeing the requests, as those
  2320. * are the ones that keep the context and ringbuffer backing objects
  2321. * pinned in place.
  2322. */
  2323. while (!list_empty(&ring->execlist_queue)) {
  2324. struct drm_i915_gem_request *submit_req;
  2325. submit_req = list_first_entry(&ring->execlist_queue,
  2326. struct drm_i915_gem_request,
  2327. execlist_link);
  2328. list_del(&submit_req->execlist_link);
  2329. if (submit_req->ctx != ring->default_context)
  2330. intel_lr_context_unpin(submit_req);
  2331. i915_gem_request_unreference(submit_req);
  2332. }
  2333. /*
  2334. * We must free the requests after all the corresponding objects have
  2335. * been moved off active lists. Which is the same order as the normal
  2336. * retire_requests function does. This is important if object hold
  2337. * implicit references on things like e.g. ppgtt address spaces through
  2338. * the request.
  2339. */
  2340. while (!list_empty(&ring->request_list)) {
  2341. struct drm_i915_gem_request *request;
  2342. request = list_first_entry(&ring->request_list,
  2343. struct drm_i915_gem_request,
  2344. list);
  2345. i915_gem_request_retire(request);
  2346. }
  2347. }
  2348. void i915_gem_reset(struct drm_device *dev)
  2349. {
  2350. struct drm_i915_private *dev_priv = dev->dev_private;
  2351. struct intel_engine_cs *ring;
  2352. int i;
  2353. /*
  2354. * Before we free the objects from the requests, we need to inspect
  2355. * them for finding the guilty party. As the requests only borrow
  2356. * their reference to the objects, the inspection must be done first.
  2357. */
  2358. for_each_ring(ring, dev_priv, i)
  2359. i915_gem_reset_ring_status(dev_priv, ring);
  2360. for_each_ring(ring, dev_priv, i)
  2361. i915_gem_reset_ring_cleanup(dev_priv, ring);
  2362. i915_gem_context_reset(dev);
  2363. i915_gem_restore_fences(dev);
  2364. WARN_ON(i915_verify_lists(dev));
  2365. }
  2366. /**
  2367. * This function clears the request list as sequence numbers are passed.
  2368. */
  2369. void
  2370. i915_gem_retire_requests_ring(struct intel_engine_cs *ring)
  2371. {
  2372. WARN_ON(i915_verify_lists(ring->dev));
  2373. /* Retire requests first as we use it above for the early return.
  2374. * If we retire requests last, we may use a later seqno and so clear
  2375. * the requests lists without clearing the active list, leading to
  2376. * confusion.
  2377. */
  2378. while (!list_empty(&ring->request_list)) {
  2379. struct drm_i915_gem_request *request;
  2380. request = list_first_entry(&ring->request_list,
  2381. struct drm_i915_gem_request,
  2382. list);
  2383. if (!i915_gem_request_completed(request, true))
  2384. break;
  2385. i915_gem_request_retire(request);
  2386. }
  2387. /* Move any buffers on the active list that are no longer referenced
  2388. * by the ringbuffer to the flushing/inactive lists as appropriate,
  2389. * before we free the context associated with the requests.
  2390. */
  2391. while (!list_empty(&ring->active_list)) {
  2392. struct drm_i915_gem_object *obj;
  2393. obj = list_first_entry(&ring->active_list,
  2394. struct drm_i915_gem_object,
  2395. ring_list[ring->id]);
  2396. if (!list_empty(&obj->last_read_req[ring->id]->list))
  2397. break;
  2398. i915_gem_object_retire__read(obj, ring->id);
  2399. }
  2400. if (unlikely(ring->trace_irq_req &&
  2401. i915_gem_request_completed(ring->trace_irq_req, true))) {
  2402. ring->irq_put(ring);
  2403. i915_gem_request_assign(&ring->trace_irq_req, NULL);
  2404. }
  2405. WARN_ON(i915_verify_lists(ring->dev));
  2406. }
  2407. bool
  2408. i915_gem_retire_requests(struct drm_device *dev)
  2409. {
  2410. struct drm_i915_private *dev_priv = dev->dev_private;
  2411. struct intel_engine_cs *ring;
  2412. bool idle = true;
  2413. int i;
  2414. for_each_ring(ring, dev_priv, i) {
  2415. i915_gem_retire_requests_ring(ring);
  2416. idle &= list_empty(&ring->request_list);
  2417. if (i915.enable_execlists) {
  2418. unsigned long flags;
  2419. spin_lock_irqsave(&ring->execlist_lock, flags);
  2420. idle &= list_empty(&ring->execlist_queue);
  2421. spin_unlock_irqrestore(&ring->execlist_lock, flags);
  2422. intel_execlists_retire_requests(ring);
  2423. }
  2424. }
  2425. if (idle)
  2426. mod_delayed_work(dev_priv->wq,
  2427. &dev_priv->mm.idle_work,
  2428. msecs_to_jiffies(100));
  2429. return idle;
  2430. }
  2431. static void
  2432. i915_gem_retire_work_handler(struct work_struct *work)
  2433. {
  2434. struct drm_i915_private *dev_priv =
  2435. container_of(work, typeof(*dev_priv), mm.retire_work.work);
  2436. struct drm_device *dev = dev_priv->dev;
  2437. bool idle;
  2438. /* Come back later if the device is busy... */
  2439. idle = false;
  2440. if (mutex_trylock(&dev->struct_mutex)) {
  2441. idle = i915_gem_retire_requests(dev);
  2442. mutex_unlock(&dev->struct_mutex);
  2443. }
  2444. if (!idle)
  2445. queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work,
  2446. round_jiffies_up_relative(HZ));
  2447. }
  2448. static void
  2449. i915_gem_idle_work_handler(struct work_struct *work)
  2450. {
  2451. struct drm_i915_private *dev_priv =
  2452. container_of(work, typeof(*dev_priv), mm.idle_work.work);
  2453. struct drm_device *dev = dev_priv->dev;
  2454. struct intel_engine_cs *ring;
  2455. int i;
  2456. for_each_ring(ring, dev_priv, i)
  2457. if (!list_empty(&ring->request_list))
  2458. return;
  2459. intel_mark_idle(dev);
  2460. if (mutex_trylock(&dev->struct_mutex)) {
  2461. struct intel_engine_cs *ring;
  2462. int i;
  2463. for_each_ring(ring, dev_priv, i)
  2464. i915_gem_batch_pool_fini(&ring->batch_pool);
  2465. mutex_unlock(&dev->struct_mutex);
  2466. }
  2467. }
  2468. /**
  2469. * Ensures that an object will eventually get non-busy by flushing any required
  2470. * write domains, emitting any outstanding lazy request and retiring and
  2471. * completed requests.
  2472. */
  2473. static int
  2474. i915_gem_object_flush_active(struct drm_i915_gem_object *obj)
  2475. {
  2476. int i;
  2477. if (!obj->active)
  2478. return 0;
  2479. for (i = 0; i < I915_NUM_RINGS; i++) {
  2480. struct drm_i915_gem_request *req;
  2481. req = obj->last_read_req[i];
  2482. if (req == NULL)
  2483. continue;
  2484. if (list_empty(&req->list))
  2485. goto retire;
  2486. if (i915_gem_request_completed(req, true)) {
  2487. __i915_gem_request_retire__upto(req);
  2488. retire:
  2489. i915_gem_object_retire__read(obj, i);
  2490. }
  2491. }
  2492. return 0;
  2493. }
  2494. /**
  2495. * i915_gem_wait_ioctl - implements DRM_IOCTL_I915_GEM_WAIT
  2496. * @DRM_IOCTL_ARGS: standard ioctl arguments
  2497. *
  2498. * Returns 0 if successful, else an error is returned with the remaining time in
  2499. * the timeout parameter.
  2500. * -ETIME: object is still busy after timeout
  2501. * -ERESTARTSYS: signal interrupted the wait
  2502. * -ENONENT: object doesn't exist
  2503. * Also possible, but rare:
  2504. * -EAGAIN: GPU wedged
  2505. * -ENOMEM: damn
  2506. * -ENODEV: Internal IRQ fail
  2507. * -E?: The add request failed
  2508. *
  2509. * The wait ioctl with a timeout of 0 reimplements the busy ioctl. With any
  2510. * non-zero timeout parameter the wait ioctl will wait for the given number of
  2511. * nanoseconds on an object becoming unbusy. Since the wait itself does so
  2512. * without holding struct_mutex the object may become re-busied before this
  2513. * function completes. A similar but shorter * race condition exists in the busy
  2514. * ioctl
  2515. */
  2516. int
  2517. i915_gem_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
  2518. {
  2519. struct drm_i915_private *dev_priv = dev->dev_private;
  2520. struct drm_i915_gem_wait *args = data;
  2521. struct drm_i915_gem_object *obj;
  2522. struct drm_i915_gem_request *req[I915_NUM_RINGS];
  2523. unsigned reset_counter;
  2524. int i, n = 0;
  2525. int ret;
  2526. if (args->flags != 0)
  2527. return -EINVAL;
  2528. ret = i915_mutex_lock_interruptible(dev);
  2529. if (ret)
  2530. return ret;
  2531. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->bo_handle));
  2532. if (&obj->base == NULL) {
  2533. mutex_unlock(&dev->struct_mutex);
  2534. return -ENOENT;
  2535. }
  2536. /* Need to make sure the object gets inactive eventually. */
  2537. ret = i915_gem_object_flush_active(obj);
  2538. if (ret)
  2539. goto out;
  2540. if (!obj->active)
  2541. goto out;
  2542. /* Do this after OLR check to make sure we make forward progress polling
  2543. * on this IOCTL with a timeout == 0 (like busy ioctl)
  2544. */
  2545. if (args->timeout_ns == 0) {
  2546. ret = -ETIME;
  2547. goto out;
  2548. }
  2549. drm_gem_object_unreference(&obj->base);
  2550. reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
  2551. for (i = 0; i < I915_NUM_RINGS; i++) {
  2552. if (obj->last_read_req[i] == NULL)
  2553. continue;
  2554. req[n++] = i915_gem_request_reference(obj->last_read_req[i]);
  2555. }
  2556. mutex_unlock(&dev->struct_mutex);
  2557. for (i = 0; i < n; i++) {
  2558. if (ret == 0)
  2559. ret = __i915_wait_request(req[i], reset_counter, true,
  2560. args->timeout_ns > 0 ? &args->timeout_ns : NULL,
  2561. file->driver_priv);
  2562. i915_gem_request_unreference__unlocked(req[i]);
  2563. }
  2564. return ret;
  2565. out:
  2566. drm_gem_object_unreference(&obj->base);
  2567. mutex_unlock(&dev->struct_mutex);
  2568. return ret;
  2569. }
  2570. static int
  2571. __i915_gem_object_sync(struct drm_i915_gem_object *obj,
  2572. struct intel_engine_cs *to,
  2573. struct drm_i915_gem_request *from_req,
  2574. struct drm_i915_gem_request **to_req)
  2575. {
  2576. struct intel_engine_cs *from;
  2577. int ret;
  2578. from = i915_gem_request_get_ring(from_req);
  2579. if (to == from)
  2580. return 0;
  2581. if (i915_gem_request_completed(from_req, true))
  2582. return 0;
  2583. if (!i915_semaphore_is_enabled(obj->base.dev)) {
  2584. struct drm_i915_private *i915 = to_i915(obj->base.dev);
  2585. ret = __i915_wait_request(from_req,
  2586. atomic_read(&i915->gpu_error.reset_counter),
  2587. i915->mm.interruptible,
  2588. NULL,
  2589. &i915->rps.semaphores);
  2590. if (ret)
  2591. return ret;
  2592. i915_gem_object_retire_request(obj, from_req);
  2593. } else {
  2594. int idx = intel_ring_sync_index(from, to);
  2595. u32 seqno = i915_gem_request_get_seqno(from_req);
  2596. WARN_ON(!to_req);
  2597. if (seqno <= from->semaphore.sync_seqno[idx])
  2598. return 0;
  2599. if (*to_req == NULL) {
  2600. ret = i915_gem_request_alloc(to, to->default_context, to_req);
  2601. if (ret)
  2602. return ret;
  2603. }
  2604. trace_i915_gem_ring_sync_to(*to_req, from, from_req);
  2605. ret = to->semaphore.sync_to(*to_req, from, seqno);
  2606. if (ret)
  2607. return ret;
  2608. /* We use last_read_req because sync_to()
  2609. * might have just caused seqno wrap under
  2610. * the radar.
  2611. */
  2612. from->semaphore.sync_seqno[idx] =
  2613. i915_gem_request_get_seqno(obj->last_read_req[from->id]);
  2614. }
  2615. return 0;
  2616. }
  2617. /**
  2618. * i915_gem_object_sync - sync an object to a ring.
  2619. *
  2620. * @obj: object which may be in use on another ring.
  2621. * @to: ring we wish to use the object on. May be NULL.
  2622. * @to_req: request we wish to use the object for. See below.
  2623. * This will be allocated and returned if a request is
  2624. * required but not passed in.
  2625. *
  2626. * This code is meant to abstract object synchronization with the GPU.
  2627. * Calling with NULL implies synchronizing the object with the CPU
  2628. * rather than a particular GPU ring. Conceptually we serialise writes
  2629. * between engines inside the GPU. We only allow one engine to write
  2630. * into a buffer at any time, but multiple readers. To ensure each has
  2631. * a coherent view of memory, we must:
  2632. *
  2633. * - If there is an outstanding write request to the object, the new
  2634. * request must wait for it to complete (either CPU or in hw, requests
  2635. * on the same ring will be naturally ordered).
  2636. *
  2637. * - If we are a write request (pending_write_domain is set), the new
  2638. * request must wait for outstanding read requests to complete.
  2639. *
  2640. * For CPU synchronisation (NULL to) no request is required. For syncing with
  2641. * rings to_req must be non-NULL. However, a request does not have to be
  2642. * pre-allocated. If *to_req is NULL and sync commands will be emitted then a
  2643. * request will be allocated automatically and returned through *to_req. Note
  2644. * that it is not guaranteed that commands will be emitted (because the system
  2645. * might already be idle). Hence there is no need to create a request that
  2646. * might never have any work submitted. Note further that if a request is
  2647. * returned in *to_req, it is the responsibility of the caller to submit
  2648. * that request (after potentially adding more work to it).
  2649. *
  2650. * Returns 0 if successful, else propagates up the lower layer error.
  2651. */
  2652. int
  2653. i915_gem_object_sync(struct drm_i915_gem_object *obj,
  2654. struct intel_engine_cs *to,
  2655. struct drm_i915_gem_request **to_req)
  2656. {
  2657. const bool readonly = obj->base.pending_write_domain == 0;
  2658. struct drm_i915_gem_request *req[I915_NUM_RINGS];
  2659. int ret, i, n;
  2660. if (!obj->active)
  2661. return 0;
  2662. if (to == NULL)
  2663. return i915_gem_object_wait_rendering(obj, readonly);
  2664. n = 0;
  2665. if (readonly) {
  2666. if (obj->last_write_req)
  2667. req[n++] = obj->last_write_req;
  2668. } else {
  2669. for (i = 0; i < I915_NUM_RINGS; i++)
  2670. if (obj->last_read_req[i])
  2671. req[n++] = obj->last_read_req[i];
  2672. }
  2673. for (i = 0; i < n; i++) {
  2674. ret = __i915_gem_object_sync(obj, to, req[i], to_req);
  2675. if (ret)
  2676. return ret;
  2677. }
  2678. return 0;
  2679. }
  2680. static void i915_gem_object_finish_gtt(struct drm_i915_gem_object *obj)
  2681. {
  2682. u32 old_write_domain, old_read_domains;
  2683. /* Force a pagefault for domain tracking on next user access */
  2684. i915_gem_release_mmap(obj);
  2685. if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0)
  2686. return;
  2687. /* Wait for any direct GTT access to complete */
  2688. mb();
  2689. old_read_domains = obj->base.read_domains;
  2690. old_write_domain = obj->base.write_domain;
  2691. obj->base.read_domains &= ~I915_GEM_DOMAIN_GTT;
  2692. obj->base.write_domain &= ~I915_GEM_DOMAIN_GTT;
  2693. trace_i915_gem_object_change_domain(obj,
  2694. old_read_domains,
  2695. old_write_domain);
  2696. }
  2697. static int __i915_vma_unbind(struct i915_vma *vma, bool wait)
  2698. {
  2699. struct drm_i915_gem_object *obj = vma->obj;
  2700. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  2701. int ret;
  2702. if (list_empty(&vma->vma_link))
  2703. return 0;
  2704. if (!drm_mm_node_allocated(&vma->node)) {
  2705. i915_gem_vma_destroy(vma);
  2706. return 0;
  2707. }
  2708. if (vma->pin_count)
  2709. return -EBUSY;
  2710. BUG_ON(obj->pages == NULL);
  2711. if (wait) {
  2712. ret = i915_gem_object_wait_rendering(obj, false);
  2713. if (ret)
  2714. return ret;
  2715. }
  2716. if (i915_is_ggtt(vma->vm) &&
  2717. vma->ggtt_view.type == I915_GGTT_VIEW_NORMAL) {
  2718. i915_gem_object_finish_gtt(obj);
  2719. /* release the fence reg _after_ flushing */
  2720. ret = i915_gem_object_put_fence(obj);
  2721. if (ret)
  2722. return ret;
  2723. }
  2724. trace_i915_vma_unbind(vma);
  2725. vma->vm->unbind_vma(vma);
  2726. vma->bound = 0;
  2727. list_del_init(&vma->mm_list);
  2728. if (i915_is_ggtt(vma->vm)) {
  2729. if (vma->ggtt_view.type == I915_GGTT_VIEW_NORMAL) {
  2730. obj->map_and_fenceable = false;
  2731. } else if (vma->ggtt_view.pages) {
  2732. sg_free_table(vma->ggtt_view.pages);
  2733. kfree(vma->ggtt_view.pages);
  2734. }
  2735. vma->ggtt_view.pages = NULL;
  2736. }
  2737. drm_mm_remove_node(&vma->node);
  2738. i915_gem_vma_destroy(vma);
  2739. /* Since the unbound list is global, only move to that list if
  2740. * no more VMAs exist. */
  2741. if (list_empty(&obj->vma_list))
  2742. list_move_tail(&obj->global_list, &dev_priv->mm.unbound_list);
  2743. /* And finally now the object is completely decoupled from this vma,
  2744. * we can drop its hold on the backing storage and allow it to be
  2745. * reaped by the shrinker.
  2746. */
  2747. i915_gem_object_unpin_pages(obj);
  2748. return 0;
  2749. }
  2750. int i915_vma_unbind(struct i915_vma *vma)
  2751. {
  2752. return __i915_vma_unbind(vma, true);
  2753. }
  2754. int __i915_vma_unbind_no_wait(struct i915_vma *vma)
  2755. {
  2756. return __i915_vma_unbind(vma, false);
  2757. }
  2758. int i915_gpu_idle(struct drm_device *dev)
  2759. {
  2760. struct drm_i915_private *dev_priv = dev->dev_private;
  2761. struct intel_engine_cs *ring;
  2762. int ret, i;
  2763. /* Flush everything onto the inactive list. */
  2764. for_each_ring(ring, dev_priv, i) {
  2765. if (!i915.enable_execlists) {
  2766. struct drm_i915_gem_request *req;
  2767. ret = i915_gem_request_alloc(ring, ring->default_context, &req);
  2768. if (ret)
  2769. return ret;
  2770. ret = i915_switch_context(req);
  2771. if (ret) {
  2772. i915_gem_request_cancel(req);
  2773. return ret;
  2774. }
  2775. i915_add_request_no_flush(req);
  2776. }
  2777. ret = intel_ring_idle(ring);
  2778. if (ret)
  2779. return ret;
  2780. }
  2781. WARN_ON(i915_verify_lists(dev));
  2782. return 0;
  2783. }
  2784. static bool i915_gem_valid_gtt_space(struct i915_vma *vma,
  2785. unsigned long cache_level)
  2786. {
  2787. struct drm_mm_node *gtt_space = &vma->node;
  2788. struct drm_mm_node *other;
  2789. /*
  2790. * On some machines we have to be careful when putting differing types
  2791. * of snoopable memory together to avoid the prefetcher crossing memory
  2792. * domains and dying. During vm initialisation, we decide whether or not
  2793. * these constraints apply and set the drm_mm.color_adjust
  2794. * appropriately.
  2795. */
  2796. if (vma->vm->mm.color_adjust == NULL)
  2797. return true;
  2798. if (!drm_mm_node_allocated(gtt_space))
  2799. return true;
  2800. if (list_empty(&gtt_space->node_list))
  2801. return true;
  2802. other = list_entry(gtt_space->node_list.prev, struct drm_mm_node, node_list);
  2803. if (other->allocated && !other->hole_follows && other->color != cache_level)
  2804. return false;
  2805. other = list_entry(gtt_space->node_list.next, struct drm_mm_node, node_list);
  2806. if (other->allocated && !gtt_space->hole_follows && other->color != cache_level)
  2807. return false;
  2808. return true;
  2809. }
  2810. /**
  2811. * Finds free space in the GTT aperture and binds the object or a view of it
  2812. * there.
  2813. */
  2814. static struct i915_vma *
  2815. i915_gem_object_bind_to_vm(struct drm_i915_gem_object *obj,
  2816. struct i915_address_space *vm,
  2817. const struct i915_ggtt_view *ggtt_view,
  2818. unsigned alignment,
  2819. uint64_t flags)
  2820. {
  2821. struct drm_device *dev = obj->base.dev;
  2822. struct drm_i915_private *dev_priv = dev->dev_private;
  2823. u32 fence_alignment, unfenced_alignment;
  2824. u32 search_flag, alloc_flag;
  2825. u64 start, end;
  2826. u64 size, fence_size;
  2827. struct i915_vma *vma;
  2828. int ret;
  2829. if (i915_is_ggtt(vm)) {
  2830. u32 view_size;
  2831. if (WARN_ON(!ggtt_view))
  2832. return ERR_PTR(-EINVAL);
  2833. view_size = i915_ggtt_view_size(obj, ggtt_view);
  2834. fence_size = i915_gem_get_gtt_size(dev,
  2835. view_size,
  2836. obj->tiling_mode);
  2837. fence_alignment = i915_gem_get_gtt_alignment(dev,
  2838. view_size,
  2839. obj->tiling_mode,
  2840. true);
  2841. unfenced_alignment = i915_gem_get_gtt_alignment(dev,
  2842. view_size,
  2843. obj->tiling_mode,
  2844. false);
  2845. size = flags & PIN_MAPPABLE ? fence_size : view_size;
  2846. } else {
  2847. fence_size = i915_gem_get_gtt_size(dev,
  2848. obj->base.size,
  2849. obj->tiling_mode);
  2850. fence_alignment = i915_gem_get_gtt_alignment(dev,
  2851. obj->base.size,
  2852. obj->tiling_mode,
  2853. true);
  2854. unfenced_alignment =
  2855. i915_gem_get_gtt_alignment(dev,
  2856. obj->base.size,
  2857. obj->tiling_mode,
  2858. false);
  2859. size = flags & PIN_MAPPABLE ? fence_size : obj->base.size;
  2860. }
  2861. start = flags & PIN_OFFSET_BIAS ? flags & PIN_OFFSET_MASK : 0;
  2862. end = vm->total;
  2863. if (flags & PIN_MAPPABLE)
  2864. end = min_t(u64, end, dev_priv->gtt.mappable_end);
  2865. if (flags & PIN_ZONE_4G)
  2866. end = min_t(u64, end, (1ULL << 32));
  2867. if (alignment == 0)
  2868. alignment = flags & PIN_MAPPABLE ? fence_alignment :
  2869. unfenced_alignment;
  2870. if (flags & PIN_MAPPABLE && alignment & (fence_alignment - 1)) {
  2871. DRM_DEBUG("Invalid object (view type=%u) alignment requested %u\n",
  2872. ggtt_view ? ggtt_view->type : 0,
  2873. alignment);
  2874. return ERR_PTR(-EINVAL);
  2875. }
  2876. /* If binding the object/GGTT view requires more space than the entire
  2877. * aperture has, reject it early before evicting everything in a vain
  2878. * attempt to find space.
  2879. */
  2880. if (size > end) {
  2881. DRM_DEBUG("Attempting to bind an object (view type=%u) larger than the aperture: size=%llu > %s aperture=%llu\n",
  2882. ggtt_view ? ggtt_view->type : 0,
  2883. size,
  2884. flags & PIN_MAPPABLE ? "mappable" : "total",
  2885. end);
  2886. return ERR_PTR(-E2BIG);
  2887. }
  2888. ret = i915_gem_object_get_pages(obj);
  2889. if (ret)
  2890. return ERR_PTR(ret);
  2891. i915_gem_object_pin_pages(obj);
  2892. vma = ggtt_view ? i915_gem_obj_lookup_or_create_ggtt_vma(obj, ggtt_view) :
  2893. i915_gem_obj_lookup_or_create_vma(obj, vm);
  2894. if (IS_ERR(vma))
  2895. goto err_unpin;
  2896. if (flags & PIN_HIGH) {
  2897. search_flag = DRM_MM_SEARCH_BELOW;
  2898. alloc_flag = DRM_MM_CREATE_TOP;
  2899. } else {
  2900. search_flag = DRM_MM_SEARCH_DEFAULT;
  2901. alloc_flag = DRM_MM_CREATE_DEFAULT;
  2902. }
  2903. search_free:
  2904. ret = drm_mm_insert_node_in_range_generic(&vm->mm, &vma->node,
  2905. size, alignment,
  2906. obj->cache_level,
  2907. start, end,
  2908. search_flag,
  2909. alloc_flag);
  2910. if (ret) {
  2911. ret = i915_gem_evict_something(dev, vm, size, alignment,
  2912. obj->cache_level,
  2913. start, end,
  2914. flags);
  2915. if (ret == 0)
  2916. goto search_free;
  2917. goto err_free_vma;
  2918. }
  2919. if (WARN_ON(!i915_gem_valid_gtt_space(vma, obj->cache_level))) {
  2920. ret = -EINVAL;
  2921. goto err_remove_node;
  2922. }
  2923. trace_i915_vma_bind(vma, flags);
  2924. ret = i915_vma_bind(vma, obj->cache_level, flags);
  2925. if (ret)
  2926. goto err_remove_node;
  2927. list_move_tail(&obj->global_list, &dev_priv->mm.bound_list);
  2928. list_add_tail(&vma->mm_list, &vm->inactive_list);
  2929. return vma;
  2930. err_remove_node:
  2931. drm_mm_remove_node(&vma->node);
  2932. err_free_vma:
  2933. i915_gem_vma_destroy(vma);
  2934. vma = ERR_PTR(ret);
  2935. err_unpin:
  2936. i915_gem_object_unpin_pages(obj);
  2937. return vma;
  2938. }
  2939. bool
  2940. i915_gem_clflush_object(struct drm_i915_gem_object *obj,
  2941. bool force)
  2942. {
  2943. /* If we don't have a page list set up, then we're not pinned
  2944. * to GPU, and we can ignore the cache flush because it'll happen
  2945. * again at bind time.
  2946. */
  2947. if (obj->pages == NULL)
  2948. return false;
  2949. /*
  2950. * Stolen memory is always coherent with the GPU as it is explicitly
  2951. * marked as wc by the system, or the system is cache-coherent.
  2952. */
  2953. if (obj->stolen || obj->phys_handle)
  2954. return false;
  2955. /* If the GPU is snooping the contents of the CPU cache,
  2956. * we do not need to manually clear the CPU cache lines. However,
  2957. * the caches are only snooped when the render cache is
  2958. * flushed/invalidated. As we always have to emit invalidations
  2959. * and flushes when moving into and out of the RENDER domain, correct
  2960. * snooping behaviour occurs naturally as the result of our domain
  2961. * tracking.
  2962. */
  2963. if (!force && cpu_cache_is_coherent(obj->base.dev, obj->cache_level)) {
  2964. obj->cache_dirty = true;
  2965. return false;
  2966. }
  2967. trace_i915_gem_object_clflush(obj);
  2968. drm_clflush_sg(obj->pages);
  2969. obj->cache_dirty = false;
  2970. return true;
  2971. }
  2972. /** Flushes the GTT write domain for the object if it's dirty. */
  2973. static void
  2974. i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj)
  2975. {
  2976. uint32_t old_write_domain;
  2977. if (obj->base.write_domain != I915_GEM_DOMAIN_GTT)
  2978. return;
  2979. /* No actual flushing is required for the GTT write domain. Writes
  2980. * to it immediately go to main memory as far as we know, so there's
  2981. * no chipset flush. It also doesn't land in render cache.
  2982. *
  2983. * However, we do have to enforce the order so that all writes through
  2984. * the GTT land before any writes to the device, such as updates to
  2985. * the GATT itself.
  2986. */
  2987. wmb();
  2988. old_write_domain = obj->base.write_domain;
  2989. obj->base.write_domain = 0;
  2990. intel_fb_obj_flush(obj, false, ORIGIN_GTT);
  2991. trace_i915_gem_object_change_domain(obj,
  2992. obj->base.read_domains,
  2993. old_write_domain);
  2994. }
  2995. /** Flushes the CPU write domain for the object if it's dirty. */
  2996. static void
  2997. i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj)
  2998. {
  2999. uint32_t old_write_domain;
  3000. if (obj->base.write_domain != I915_GEM_DOMAIN_CPU)
  3001. return;
  3002. if (i915_gem_clflush_object(obj, obj->pin_display))
  3003. i915_gem_chipset_flush(obj->base.dev);
  3004. old_write_domain = obj->base.write_domain;
  3005. obj->base.write_domain = 0;
  3006. intel_fb_obj_flush(obj, false, ORIGIN_CPU);
  3007. trace_i915_gem_object_change_domain(obj,
  3008. obj->base.read_domains,
  3009. old_write_domain);
  3010. }
  3011. /**
  3012. * Moves a single object to the GTT read, and possibly write domain.
  3013. *
  3014. * This function returns when the move is complete, including waiting on
  3015. * flushes to occur.
  3016. */
  3017. int
  3018. i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write)
  3019. {
  3020. uint32_t old_write_domain, old_read_domains;
  3021. struct i915_vma *vma;
  3022. int ret;
  3023. if (obj->base.write_domain == I915_GEM_DOMAIN_GTT)
  3024. return 0;
  3025. ret = i915_gem_object_wait_rendering(obj, !write);
  3026. if (ret)
  3027. return ret;
  3028. /* Flush and acquire obj->pages so that we are coherent through
  3029. * direct access in memory with previous cached writes through
  3030. * shmemfs and that our cache domain tracking remains valid.
  3031. * For example, if the obj->filp was moved to swap without us
  3032. * being notified and releasing the pages, we would mistakenly
  3033. * continue to assume that the obj remained out of the CPU cached
  3034. * domain.
  3035. */
  3036. ret = i915_gem_object_get_pages(obj);
  3037. if (ret)
  3038. return ret;
  3039. i915_gem_object_flush_cpu_write_domain(obj);
  3040. /* Serialise direct access to this object with the barriers for
  3041. * coherent writes from the GPU, by effectively invalidating the
  3042. * GTT domain upon first access.
  3043. */
  3044. if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0)
  3045. mb();
  3046. old_write_domain = obj->base.write_domain;
  3047. old_read_domains = obj->base.read_domains;
  3048. /* It should now be out of any other write domains, and we can update
  3049. * the domain values for our changes.
  3050. */
  3051. BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
  3052. obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
  3053. if (write) {
  3054. obj->base.read_domains = I915_GEM_DOMAIN_GTT;
  3055. obj->base.write_domain = I915_GEM_DOMAIN_GTT;
  3056. obj->dirty = 1;
  3057. }
  3058. trace_i915_gem_object_change_domain(obj,
  3059. old_read_domains,
  3060. old_write_domain);
  3061. /* And bump the LRU for this access */
  3062. vma = i915_gem_obj_to_ggtt(obj);
  3063. if (vma && drm_mm_node_allocated(&vma->node) && !obj->active)
  3064. list_move_tail(&vma->mm_list,
  3065. &to_i915(obj->base.dev)->gtt.base.inactive_list);
  3066. return 0;
  3067. }
  3068. /**
  3069. * Changes the cache-level of an object across all VMA.
  3070. *
  3071. * After this function returns, the object will be in the new cache-level
  3072. * across all GTT and the contents of the backing storage will be coherent,
  3073. * with respect to the new cache-level. In order to keep the backing storage
  3074. * coherent for all users, we only allow a single cache level to be set
  3075. * globally on the object and prevent it from being changed whilst the
  3076. * hardware is reading from the object. That is if the object is currently
  3077. * on the scanout it will be set to uncached (or equivalent display
  3078. * cache coherency) and all non-MOCS GPU access will also be uncached so
  3079. * that all direct access to the scanout remains coherent.
  3080. */
  3081. int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj,
  3082. enum i915_cache_level cache_level)
  3083. {
  3084. struct drm_device *dev = obj->base.dev;
  3085. struct i915_vma *vma, *next;
  3086. bool bound = false;
  3087. int ret = 0;
  3088. if (obj->cache_level == cache_level)
  3089. goto out;
  3090. /* Inspect the list of currently bound VMA and unbind any that would
  3091. * be invalid given the new cache-level. This is principally to
  3092. * catch the issue of the CS prefetch crossing page boundaries and
  3093. * reading an invalid PTE on older architectures.
  3094. */
  3095. list_for_each_entry_safe(vma, next, &obj->vma_list, vma_link) {
  3096. if (!drm_mm_node_allocated(&vma->node))
  3097. continue;
  3098. if (vma->pin_count) {
  3099. DRM_DEBUG("can not change the cache level of pinned objects\n");
  3100. return -EBUSY;
  3101. }
  3102. if (!i915_gem_valid_gtt_space(vma, cache_level)) {
  3103. ret = i915_vma_unbind(vma);
  3104. if (ret)
  3105. return ret;
  3106. } else
  3107. bound = true;
  3108. }
  3109. /* We can reuse the existing drm_mm nodes but need to change the
  3110. * cache-level on the PTE. We could simply unbind them all and
  3111. * rebind with the correct cache-level on next use. However since
  3112. * we already have a valid slot, dma mapping, pages etc, we may as
  3113. * rewrite the PTE in the belief that doing so tramples upon less
  3114. * state and so involves less work.
  3115. */
  3116. if (bound) {
  3117. /* Before we change the PTE, the GPU must not be accessing it.
  3118. * If we wait upon the object, we know that all the bound
  3119. * VMA are no longer active.
  3120. */
  3121. ret = i915_gem_object_wait_rendering(obj, false);
  3122. if (ret)
  3123. return ret;
  3124. if (!HAS_LLC(dev) && cache_level != I915_CACHE_NONE) {
  3125. /* Access to snoopable pages through the GTT is
  3126. * incoherent and on some machines causes a hard
  3127. * lockup. Relinquish the CPU mmaping to force
  3128. * userspace to refault in the pages and we can
  3129. * then double check if the GTT mapping is still
  3130. * valid for that pointer access.
  3131. */
  3132. i915_gem_release_mmap(obj);
  3133. /* As we no longer need a fence for GTT access,
  3134. * we can relinquish it now (and so prevent having
  3135. * to steal a fence from someone else on the next
  3136. * fence request). Note GPU activity would have
  3137. * dropped the fence as all snoopable access is
  3138. * supposed to be linear.
  3139. */
  3140. ret = i915_gem_object_put_fence(obj);
  3141. if (ret)
  3142. return ret;
  3143. } else {
  3144. /* We either have incoherent backing store and
  3145. * so no GTT access or the architecture is fully
  3146. * coherent. In such cases, existing GTT mmaps
  3147. * ignore the cache bit in the PTE and we can
  3148. * rewrite it without confusing the GPU or having
  3149. * to force userspace to fault back in its mmaps.
  3150. */
  3151. }
  3152. list_for_each_entry(vma, &obj->vma_list, vma_link) {
  3153. if (!drm_mm_node_allocated(&vma->node))
  3154. continue;
  3155. ret = i915_vma_bind(vma, cache_level, PIN_UPDATE);
  3156. if (ret)
  3157. return ret;
  3158. }
  3159. }
  3160. list_for_each_entry(vma, &obj->vma_list, vma_link)
  3161. vma->node.color = cache_level;
  3162. obj->cache_level = cache_level;
  3163. out:
  3164. /* Flush the dirty CPU caches to the backing storage so that the
  3165. * object is now coherent at its new cache level (with respect
  3166. * to the access domain).
  3167. */
  3168. if (obj->cache_dirty &&
  3169. obj->base.write_domain != I915_GEM_DOMAIN_CPU &&
  3170. cpu_write_needs_clflush(obj)) {
  3171. if (i915_gem_clflush_object(obj, true))
  3172. i915_gem_chipset_flush(obj->base.dev);
  3173. }
  3174. return 0;
  3175. }
  3176. int i915_gem_get_caching_ioctl(struct drm_device *dev, void *data,
  3177. struct drm_file *file)
  3178. {
  3179. struct drm_i915_gem_caching *args = data;
  3180. struct drm_i915_gem_object *obj;
  3181. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  3182. if (&obj->base == NULL)
  3183. return -ENOENT;
  3184. switch (obj->cache_level) {
  3185. case I915_CACHE_LLC:
  3186. case I915_CACHE_L3_LLC:
  3187. args->caching = I915_CACHING_CACHED;
  3188. break;
  3189. case I915_CACHE_WT:
  3190. args->caching = I915_CACHING_DISPLAY;
  3191. break;
  3192. default:
  3193. args->caching = I915_CACHING_NONE;
  3194. break;
  3195. }
  3196. drm_gem_object_unreference_unlocked(&obj->base);
  3197. return 0;
  3198. }
  3199. int i915_gem_set_caching_ioctl(struct drm_device *dev, void *data,
  3200. struct drm_file *file)
  3201. {
  3202. struct drm_i915_private *dev_priv = dev->dev_private;
  3203. struct drm_i915_gem_caching *args = data;
  3204. struct drm_i915_gem_object *obj;
  3205. enum i915_cache_level level;
  3206. int ret;
  3207. switch (args->caching) {
  3208. case I915_CACHING_NONE:
  3209. level = I915_CACHE_NONE;
  3210. break;
  3211. case I915_CACHING_CACHED:
  3212. /*
  3213. * Due to a HW issue on BXT A stepping, GPU stores via a
  3214. * snooped mapping may leave stale data in a corresponding CPU
  3215. * cacheline, whereas normally such cachelines would get
  3216. * invalidated.
  3217. */
  3218. if (IS_BROXTON(dev) && INTEL_REVID(dev) < BXT_REVID_B0)
  3219. return -ENODEV;
  3220. level = I915_CACHE_LLC;
  3221. break;
  3222. case I915_CACHING_DISPLAY:
  3223. level = HAS_WT(dev) ? I915_CACHE_WT : I915_CACHE_NONE;
  3224. break;
  3225. default:
  3226. return -EINVAL;
  3227. }
  3228. intel_runtime_pm_get(dev_priv);
  3229. ret = i915_mutex_lock_interruptible(dev);
  3230. if (ret)
  3231. goto rpm_put;
  3232. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  3233. if (&obj->base == NULL) {
  3234. ret = -ENOENT;
  3235. goto unlock;
  3236. }
  3237. ret = i915_gem_object_set_cache_level(obj, level);
  3238. drm_gem_object_unreference(&obj->base);
  3239. unlock:
  3240. mutex_unlock(&dev->struct_mutex);
  3241. rpm_put:
  3242. intel_runtime_pm_put(dev_priv);
  3243. return ret;
  3244. }
  3245. /*
  3246. * Prepare buffer for display plane (scanout, cursors, etc).
  3247. * Can be called from an uninterruptible phase (modesetting) and allows
  3248. * any flushes to be pipelined (for pageflips).
  3249. */
  3250. int
  3251. i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj,
  3252. u32 alignment,
  3253. struct intel_engine_cs *pipelined,
  3254. struct drm_i915_gem_request **pipelined_request,
  3255. const struct i915_ggtt_view *view)
  3256. {
  3257. u32 old_read_domains, old_write_domain;
  3258. int ret;
  3259. ret = i915_gem_object_sync(obj, pipelined, pipelined_request);
  3260. if (ret)
  3261. return ret;
  3262. /* Mark the pin_display early so that we account for the
  3263. * display coherency whilst setting up the cache domains.
  3264. */
  3265. obj->pin_display++;
  3266. /* The display engine is not coherent with the LLC cache on gen6. As
  3267. * a result, we make sure that the pinning that is about to occur is
  3268. * done with uncached PTEs. This is lowest common denominator for all
  3269. * chipsets.
  3270. *
  3271. * However for gen6+, we could do better by using the GFDT bit instead
  3272. * of uncaching, which would allow us to flush all the LLC-cached data
  3273. * with that bit in the PTE to main memory with just one PIPE_CONTROL.
  3274. */
  3275. ret = i915_gem_object_set_cache_level(obj,
  3276. HAS_WT(obj->base.dev) ? I915_CACHE_WT : I915_CACHE_NONE);
  3277. if (ret)
  3278. goto err_unpin_display;
  3279. /* As the user may map the buffer once pinned in the display plane
  3280. * (e.g. libkms for the bootup splash), we have to ensure that we
  3281. * always use map_and_fenceable for all scanout buffers.
  3282. */
  3283. ret = i915_gem_object_ggtt_pin(obj, view, alignment,
  3284. view->type == I915_GGTT_VIEW_NORMAL ?
  3285. PIN_MAPPABLE : 0);
  3286. if (ret)
  3287. goto err_unpin_display;
  3288. i915_gem_object_flush_cpu_write_domain(obj);
  3289. old_write_domain = obj->base.write_domain;
  3290. old_read_domains = obj->base.read_domains;
  3291. /* It should now be out of any other write domains, and we can update
  3292. * the domain values for our changes.
  3293. */
  3294. obj->base.write_domain = 0;
  3295. obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
  3296. trace_i915_gem_object_change_domain(obj,
  3297. old_read_domains,
  3298. old_write_domain);
  3299. return 0;
  3300. err_unpin_display:
  3301. obj->pin_display--;
  3302. return ret;
  3303. }
  3304. void
  3305. i915_gem_object_unpin_from_display_plane(struct drm_i915_gem_object *obj,
  3306. const struct i915_ggtt_view *view)
  3307. {
  3308. if (WARN_ON(obj->pin_display == 0))
  3309. return;
  3310. i915_gem_object_ggtt_unpin_view(obj, view);
  3311. obj->pin_display--;
  3312. }
  3313. /**
  3314. * Moves a single object to the CPU read, and possibly write domain.
  3315. *
  3316. * This function returns when the move is complete, including waiting on
  3317. * flushes to occur.
  3318. */
  3319. int
  3320. i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write)
  3321. {
  3322. uint32_t old_write_domain, old_read_domains;
  3323. int ret;
  3324. if (obj->base.write_domain == I915_GEM_DOMAIN_CPU)
  3325. return 0;
  3326. ret = i915_gem_object_wait_rendering(obj, !write);
  3327. if (ret)
  3328. return ret;
  3329. i915_gem_object_flush_gtt_write_domain(obj);
  3330. old_write_domain = obj->base.write_domain;
  3331. old_read_domains = obj->base.read_domains;
  3332. /* Flush the CPU cache if it's still invalid. */
  3333. if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0) {
  3334. i915_gem_clflush_object(obj, false);
  3335. obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
  3336. }
  3337. /* It should now be out of any other write domains, and we can update
  3338. * the domain values for our changes.
  3339. */
  3340. BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
  3341. /* If we're writing through the CPU, then the GPU read domains will
  3342. * need to be invalidated at next use.
  3343. */
  3344. if (write) {
  3345. obj->base.read_domains = I915_GEM_DOMAIN_CPU;
  3346. obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  3347. }
  3348. trace_i915_gem_object_change_domain(obj,
  3349. old_read_domains,
  3350. old_write_domain);
  3351. return 0;
  3352. }
  3353. /* Throttle our rendering by waiting until the ring has completed our requests
  3354. * emitted over 20 msec ago.
  3355. *
  3356. * Note that if we were to use the current jiffies each time around the loop,
  3357. * we wouldn't escape the function with any frames outstanding if the time to
  3358. * render a frame was over 20ms.
  3359. *
  3360. * This should get us reasonable parallelism between CPU and GPU but also
  3361. * relatively low latency when blocking on a particular request to finish.
  3362. */
  3363. static int
  3364. i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
  3365. {
  3366. struct drm_i915_private *dev_priv = dev->dev_private;
  3367. struct drm_i915_file_private *file_priv = file->driver_priv;
  3368. unsigned long recent_enough = jiffies - DRM_I915_THROTTLE_JIFFIES;
  3369. struct drm_i915_gem_request *request, *target = NULL;
  3370. unsigned reset_counter;
  3371. int ret;
  3372. ret = i915_gem_wait_for_error(&dev_priv->gpu_error);
  3373. if (ret)
  3374. return ret;
  3375. ret = i915_gem_check_wedge(&dev_priv->gpu_error, false);
  3376. if (ret)
  3377. return ret;
  3378. spin_lock(&file_priv->mm.lock);
  3379. list_for_each_entry(request, &file_priv->mm.request_list, client_list) {
  3380. if (time_after_eq(request->emitted_jiffies, recent_enough))
  3381. break;
  3382. /*
  3383. * Note that the request might not have been submitted yet.
  3384. * In which case emitted_jiffies will be zero.
  3385. */
  3386. if (!request->emitted_jiffies)
  3387. continue;
  3388. target = request;
  3389. }
  3390. reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
  3391. if (target)
  3392. i915_gem_request_reference(target);
  3393. spin_unlock(&file_priv->mm.lock);
  3394. if (target == NULL)
  3395. return 0;
  3396. ret = __i915_wait_request(target, reset_counter, true, NULL, NULL);
  3397. if (ret == 0)
  3398. queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, 0);
  3399. i915_gem_request_unreference__unlocked(target);
  3400. return ret;
  3401. }
  3402. static bool
  3403. i915_vma_misplaced(struct i915_vma *vma, uint32_t alignment, uint64_t flags)
  3404. {
  3405. struct drm_i915_gem_object *obj = vma->obj;
  3406. if (alignment &&
  3407. vma->node.start & (alignment - 1))
  3408. return true;
  3409. if (flags & PIN_MAPPABLE && !obj->map_and_fenceable)
  3410. return true;
  3411. if (flags & PIN_OFFSET_BIAS &&
  3412. vma->node.start < (flags & PIN_OFFSET_MASK))
  3413. return true;
  3414. return false;
  3415. }
  3416. void __i915_vma_set_map_and_fenceable(struct i915_vma *vma)
  3417. {
  3418. struct drm_i915_gem_object *obj = vma->obj;
  3419. bool mappable, fenceable;
  3420. u32 fence_size, fence_alignment;
  3421. fence_size = i915_gem_get_gtt_size(obj->base.dev,
  3422. obj->base.size,
  3423. obj->tiling_mode);
  3424. fence_alignment = i915_gem_get_gtt_alignment(obj->base.dev,
  3425. obj->base.size,
  3426. obj->tiling_mode,
  3427. true);
  3428. fenceable = (vma->node.size == fence_size &&
  3429. (vma->node.start & (fence_alignment - 1)) == 0);
  3430. mappable = (vma->node.start + fence_size <=
  3431. to_i915(obj->base.dev)->gtt.mappable_end);
  3432. obj->map_and_fenceable = mappable && fenceable;
  3433. }
  3434. static int
  3435. i915_gem_object_do_pin(struct drm_i915_gem_object *obj,
  3436. struct i915_address_space *vm,
  3437. const struct i915_ggtt_view *ggtt_view,
  3438. uint32_t alignment,
  3439. uint64_t flags)
  3440. {
  3441. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  3442. struct i915_vma *vma;
  3443. unsigned bound;
  3444. int ret;
  3445. if (WARN_ON(vm == &dev_priv->mm.aliasing_ppgtt->base))
  3446. return -ENODEV;
  3447. if (WARN_ON(flags & (PIN_GLOBAL | PIN_MAPPABLE) && !i915_is_ggtt(vm)))
  3448. return -EINVAL;
  3449. if (WARN_ON((flags & (PIN_MAPPABLE | PIN_GLOBAL)) == PIN_MAPPABLE))
  3450. return -EINVAL;
  3451. if (WARN_ON(i915_is_ggtt(vm) != !!ggtt_view))
  3452. return -EINVAL;
  3453. vma = ggtt_view ? i915_gem_obj_to_ggtt_view(obj, ggtt_view) :
  3454. i915_gem_obj_to_vma(obj, vm);
  3455. if (IS_ERR(vma))
  3456. return PTR_ERR(vma);
  3457. if (vma) {
  3458. if (WARN_ON(vma->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT))
  3459. return -EBUSY;
  3460. if (i915_vma_misplaced(vma, alignment, flags)) {
  3461. WARN(vma->pin_count,
  3462. "bo is already pinned in %s with incorrect alignment:"
  3463. " offset=%08x %08x, req.alignment=%x, req.map_and_fenceable=%d,"
  3464. " obj->map_and_fenceable=%d\n",
  3465. ggtt_view ? "ggtt" : "ppgtt",
  3466. upper_32_bits(vma->node.start),
  3467. lower_32_bits(vma->node.start),
  3468. alignment,
  3469. !!(flags & PIN_MAPPABLE),
  3470. obj->map_and_fenceable);
  3471. ret = i915_vma_unbind(vma);
  3472. if (ret)
  3473. return ret;
  3474. vma = NULL;
  3475. }
  3476. }
  3477. bound = vma ? vma->bound : 0;
  3478. if (vma == NULL || !drm_mm_node_allocated(&vma->node)) {
  3479. vma = i915_gem_object_bind_to_vm(obj, vm, ggtt_view, alignment,
  3480. flags);
  3481. if (IS_ERR(vma))
  3482. return PTR_ERR(vma);
  3483. } else {
  3484. ret = i915_vma_bind(vma, obj->cache_level, flags);
  3485. if (ret)
  3486. return ret;
  3487. }
  3488. if (ggtt_view && ggtt_view->type == I915_GGTT_VIEW_NORMAL &&
  3489. (bound ^ vma->bound) & GLOBAL_BIND) {
  3490. __i915_vma_set_map_and_fenceable(vma);
  3491. WARN_ON(flags & PIN_MAPPABLE && !obj->map_and_fenceable);
  3492. }
  3493. vma->pin_count++;
  3494. return 0;
  3495. }
  3496. int
  3497. i915_gem_object_pin(struct drm_i915_gem_object *obj,
  3498. struct i915_address_space *vm,
  3499. uint32_t alignment,
  3500. uint64_t flags)
  3501. {
  3502. return i915_gem_object_do_pin(obj, vm,
  3503. i915_is_ggtt(vm) ? &i915_ggtt_view_normal : NULL,
  3504. alignment, flags);
  3505. }
  3506. int
  3507. i915_gem_object_ggtt_pin(struct drm_i915_gem_object *obj,
  3508. const struct i915_ggtt_view *view,
  3509. uint32_t alignment,
  3510. uint64_t flags)
  3511. {
  3512. if (WARN_ONCE(!view, "no view specified"))
  3513. return -EINVAL;
  3514. return i915_gem_object_do_pin(obj, i915_obj_to_ggtt(obj), view,
  3515. alignment, flags | PIN_GLOBAL);
  3516. }
  3517. void
  3518. i915_gem_object_ggtt_unpin_view(struct drm_i915_gem_object *obj,
  3519. const struct i915_ggtt_view *view)
  3520. {
  3521. struct i915_vma *vma = i915_gem_obj_to_ggtt_view(obj, view);
  3522. BUG_ON(!vma);
  3523. WARN_ON(vma->pin_count == 0);
  3524. WARN_ON(!i915_gem_obj_ggtt_bound_view(obj, view));
  3525. --vma->pin_count;
  3526. }
  3527. int
  3528. i915_gem_busy_ioctl(struct drm_device *dev, void *data,
  3529. struct drm_file *file)
  3530. {
  3531. struct drm_i915_gem_busy *args = data;
  3532. struct drm_i915_gem_object *obj;
  3533. int ret;
  3534. ret = i915_mutex_lock_interruptible(dev);
  3535. if (ret)
  3536. return ret;
  3537. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  3538. if (&obj->base == NULL) {
  3539. ret = -ENOENT;
  3540. goto unlock;
  3541. }
  3542. /* Count all active objects as busy, even if they are currently not used
  3543. * by the gpu. Users of this interface expect objects to eventually
  3544. * become non-busy without any further actions, therefore emit any
  3545. * necessary flushes here.
  3546. */
  3547. ret = i915_gem_object_flush_active(obj);
  3548. if (ret)
  3549. goto unref;
  3550. BUILD_BUG_ON(I915_NUM_RINGS > 16);
  3551. args->busy = obj->active << 16;
  3552. if (obj->last_write_req)
  3553. args->busy |= obj->last_write_req->ring->id;
  3554. unref:
  3555. drm_gem_object_unreference(&obj->base);
  3556. unlock:
  3557. mutex_unlock(&dev->struct_mutex);
  3558. return ret;
  3559. }
  3560. int
  3561. i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
  3562. struct drm_file *file_priv)
  3563. {
  3564. return i915_gem_ring_throttle(dev, file_priv);
  3565. }
  3566. int
  3567. i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
  3568. struct drm_file *file_priv)
  3569. {
  3570. struct drm_i915_private *dev_priv = dev->dev_private;
  3571. struct drm_i915_gem_madvise *args = data;
  3572. struct drm_i915_gem_object *obj;
  3573. int ret;
  3574. switch (args->madv) {
  3575. case I915_MADV_DONTNEED:
  3576. case I915_MADV_WILLNEED:
  3577. break;
  3578. default:
  3579. return -EINVAL;
  3580. }
  3581. ret = i915_mutex_lock_interruptible(dev);
  3582. if (ret)
  3583. return ret;
  3584. obj = to_intel_bo(drm_gem_object_lookup(dev, file_priv, args->handle));
  3585. if (&obj->base == NULL) {
  3586. ret = -ENOENT;
  3587. goto unlock;
  3588. }
  3589. if (i915_gem_obj_is_pinned(obj)) {
  3590. ret = -EINVAL;
  3591. goto out;
  3592. }
  3593. if (obj->pages &&
  3594. obj->tiling_mode != I915_TILING_NONE &&
  3595. dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
  3596. if (obj->madv == I915_MADV_WILLNEED)
  3597. i915_gem_object_unpin_pages(obj);
  3598. if (args->madv == I915_MADV_WILLNEED)
  3599. i915_gem_object_pin_pages(obj);
  3600. }
  3601. if (obj->madv != __I915_MADV_PURGED)
  3602. obj->madv = args->madv;
  3603. /* if the object is no longer attached, discard its backing storage */
  3604. if (obj->madv == I915_MADV_DONTNEED && obj->pages == NULL)
  3605. i915_gem_object_truncate(obj);
  3606. args->retained = obj->madv != __I915_MADV_PURGED;
  3607. out:
  3608. drm_gem_object_unreference(&obj->base);
  3609. unlock:
  3610. mutex_unlock(&dev->struct_mutex);
  3611. return ret;
  3612. }
  3613. void i915_gem_object_init(struct drm_i915_gem_object *obj,
  3614. const struct drm_i915_gem_object_ops *ops)
  3615. {
  3616. int i;
  3617. INIT_LIST_HEAD(&obj->global_list);
  3618. for (i = 0; i < I915_NUM_RINGS; i++)
  3619. INIT_LIST_HEAD(&obj->ring_list[i]);
  3620. INIT_LIST_HEAD(&obj->obj_exec_link);
  3621. INIT_LIST_HEAD(&obj->vma_list);
  3622. INIT_LIST_HEAD(&obj->batch_pool_link);
  3623. obj->ops = ops;
  3624. obj->fence_reg = I915_FENCE_REG_NONE;
  3625. obj->madv = I915_MADV_WILLNEED;
  3626. i915_gem_info_add_obj(obj->base.dev->dev_private, obj->base.size);
  3627. }
  3628. static const struct drm_i915_gem_object_ops i915_gem_object_ops = {
  3629. .get_pages = i915_gem_object_get_pages_gtt,
  3630. .put_pages = i915_gem_object_put_pages_gtt,
  3631. };
  3632. struct drm_i915_gem_object *i915_gem_alloc_object(struct drm_device *dev,
  3633. size_t size)
  3634. {
  3635. struct drm_i915_gem_object *obj;
  3636. struct address_space *mapping;
  3637. gfp_t mask;
  3638. obj = i915_gem_object_alloc(dev);
  3639. if (obj == NULL)
  3640. return NULL;
  3641. if (drm_gem_object_init(dev, &obj->base, size) != 0) {
  3642. i915_gem_object_free(obj);
  3643. return NULL;
  3644. }
  3645. mask = GFP_HIGHUSER | __GFP_RECLAIMABLE;
  3646. if (IS_CRESTLINE(dev) || IS_BROADWATER(dev)) {
  3647. /* 965gm cannot relocate objects above 4GiB. */
  3648. mask &= ~__GFP_HIGHMEM;
  3649. mask |= __GFP_DMA32;
  3650. }
  3651. mapping = file_inode(obj->base.filp)->i_mapping;
  3652. mapping_set_gfp_mask(mapping, mask);
  3653. i915_gem_object_init(obj, &i915_gem_object_ops);
  3654. obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  3655. obj->base.read_domains = I915_GEM_DOMAIN_CPU;
  3656. if (HAS_LLC(dev)) {
  3657. /* On some devices, we can have the GPU use the LLC (the CPU
  3658. * cache) for about a 10% performance improvement
  3659. * compared to uncached. Graphics requests other than
  3660. * display scanout are coherent with the CPU in
  3661. * accessing this cache. This means in this mode we
  3662. * don't need to clflush on the CPU side, and on the
  3663. * GPU side we only need to flush internal caches to
  3664. * get data visible to the CPU.
  3665. *
  3666. * However, we maintain the display planes as UC, and so
  3667. * need to rebind when first used as such.
  3668. */
  3669. obj->cache_level = I915_CACHE_LLC;
  3670. } else
  3671. obj->cache_level = I915_CACHE_NONE;
  3672. trace_i915_gem_object_create(obj);
  3673. return obj;
  3674. }
  3675. static bool discard_backing_storage(struct drm_i915_gem_object *obj)
  3676. {
  3677. /* If we are the last user of the backing storage (be it shmemfs
  3678. * pages or stolen etc), we know that the pages are going to be
  3679. * immediately released. In this case, we can then skip copying
  3680. * back the contents from the GPU.
  3681. */
  3682. if (obj->madv != I915_MADV_WILLNEED)
  3683. return false;
  3684. if (obj->base.filp == NULL)
  3685. return true;
  3686. /* At first glance, this looks racy, but then again so would be
  3687. * userspace racing mmap against close. However, the first external
  3688. * reference to the filp can only be obtained through the
  3689. * i915_gem_mmap_ioctl() which safeguards us against the user
  3690. * acquiring such a reference whilst we are in the middle of
  3691. * freeing the object.
  3692. */
  3693. return atomic_long_read(&obj->base.filp->f_count) == 1;
  3694. }
  3695. void i915_gem_free_object(struct drm_gem_object *gem_obj)
  3696. {
  3697. struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
  3698. struct drm_device *dev = obj->base.dev;
  3699. struct drm_i915_private *dev_priv = dev->dev_private;
  3700. struct i915_vma *vma, *next;
  3701. intel_runtime_pm_get(dev_priv);
  3702. trace_i915_gem_object_destroy(obj);
  3703. list_for_each_entry_safe(vma, next, &obj->vma_list, vma_link) {
  3704. int ret;
  3705. vma->pin_count = 0;
  3706. ret = i915_vma_unbind(vma);
  3707. if (WARN_ON(ret == -ERESTARTSYS)) {
  3708. bool was_interruptible;
  3709. was_interruptible = dev_priv->mm.interruptible;
  3710. dev_priv->mm.interruptible = false;
  3711. WARN_ON(i915_vma_unbind(vma));
  3712. dev_priv->mm.interruptible = was_interruptible;
  3713. }
  3714. }
  3715. /* Stolen objects don't hold a ref, but do hold pin count. Fix that up
  3716. * before progressing. */
  3717. if (obj->stolen)
  3718. i915_gem_object_unpin_pages(obj);
  3719. WARN_ON(obj->frontbuffer_bits);
  3720. if (obj->pages && obj->madv == I915_MADV_WILLNEED &&
  3721. dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES &&
  3722. obj->tiling_mode != I915_TILING_NONE)
  3723. i915_gem_object_unpin_pages(obj);
  3724. if (WARN_ON(obj->pages_pin_count))
  3725. obj->pages_pin_count = 0;
  3726. if (discard_backing_storage(obj))
  3727. obj->madv = I915_MADV_DONTNEED;
  3728. i915_gem_object_put_pages(obj);
  3729. i915_gem_object_free_mmap_offset(obj);
  3730. BUG_ON(obj->pages);
  3731. if (obj->base.import_attach)
  3732. drm_prime_gem_destroy(&obj->base, NULL);
  3733. if (obj->ops->release)
  3734. obj->ops->release(obj);
  3735. drm_gem_object_release(&obj->base);
  3736. i915_gem_info_remove_obj(dev_priv, obj->base.size);
  3737. kfree(obj->bit_17);
  3738. i915_gem_object_free(obj);
  3739. intel_runtime_pm_put(dev_priv);
  3740. }
  3741. struct i915_vma *i915_gem_obj_to_vma(struct drm_i915_gem_object *obj,
  3742. struct i915_address_space *vm)
  3743. {
  3744. struct i915_vma *vma;
  3745. list_for_each_entry(vma, &obj->vma_list, vma_link) {
  3746. if (i915_is_ggtt(vma->vm) &&
  3747. vma->ggtt_view.type != I915_GGTT_VIEW_NORMAL)
  3748. continue;
  3749. if (vma->vm == vm)
  3750. return vma;
  3751. }
  3752. return NULL;
  3753. }
  3754. struct i915_vma *i915_gem_obj_to_ggtt_view(struct drm_i915_gem_object *obj,
  3755. const struct i915_ggtt_view *view)
  3756. {
  3757. struct i915_address_space *ggtt = i915_obj_to_ggtt(obj);
  3758. struct i915_vma *vma;
  3759. if (WARN_ONCE(!view, "no view specified"))
  3760. return ERR_PTR(-EINVAL);
  3761. list_for_each_entry(vma, &obj->vma_list, vma_link)
  3762. if (vma->vm == ggtt &&
  3763. i915_ggtt_view_equal(&vma->ggtt_view, view))
  3764. return vma;
  3765. return NULL;
  3766. }
  3767. void i915_gem_vma_destroy(struct i915_vma *vma)
  3768. {
  3769. struct i915_address_space *vm = NULL;
  3770. WARN_ON(vma->node.allocated);
  3771. /* Keep the vma as a placeholder in the execbuffer reservation lists */
  3772. if (!list_empty(&vma->exec_list))
  3773. return;
  3774. vm = vma->vm;
  3775. if (!i915_is_ggtt(vm))
  3776. i915_ppgtt_put(i915_vm_to_ppgtt(vm));
  3777. list_del(&vma->vma_link);
  3778. kmem_cache_free(to_i915(vma->obj->base.dev)->vmas, vma);
  3779. }
  3780. static void
  3781. i915_gem_stop_ringbuffers(struct drm_device *dev)
  3782. {
  3783. struct drm_i915_private *dev_priv = dev->dev_private;
  3784. struct intel_engine_cs *ring;
  3785. int i;
  3786. for_each_ring(ring, dev_priv, i)
  3787. dev_priv->gt.stop_ring(ring);
  3788. }
  3789. int
  3790. i915_gem_suspend(struct drm_device *dev)
  3791. {
  3792. struct drm_i915_private *dev_priv = dev->dev_private;
  3793. int ret = 0;
  3794. mutex_lock(&dev->struct_mutex);
  3795. ret = i915_gpu_idle(dev);
  3796. if (ret)
  3797. goto err;
  3798. i915_gem_retire_requests(dev);
  3799. i915_gem_stop_ringbuffers(dev);
  3800. mutex_unlock(&dev->struct_mutex);
  3801. cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work);
  3802. cancel_delayed_work_sync(&dev_priv->mm.retire_work);
  3803. flush_delayed_work(&dev_priv->mm.idle_work);
  3804. /* Assert that we sucessfully flushed all the work and
  3805. * reset the GPU back to its idle, low power state.
  3806. */
  3807. WARN_ON(dev_priv->mm.busy);
  3808. return 0;
  3809. err:
  3810. mutex_unlock(&dev->struct_mutex);
  3811. return ret;
  3812. }
  3813. int i915_gem_l3_remap(struct drm_i915_gem_request *req, int slice)
  3814. {
  3815. struct intel_engine_cs *ring = req->ring;
  3816. struct drm_device *dev = ring->dev;
  3817. struct drm_i915_private *dev_priv = dev->dev_private;
  3818. u32 reg_base = GEN7_L3LOG_BASE + (slice * 0x200);
  3819. u32 *remap_info = dev_priv->l3_parity.remap_info[slice];
  3820. int i, ret;
  3821. if (!HAS_L3_DPF(dev) || !remap_info)
  3822. return 0;
  3823. ret = intel_ring_begin(req, GEN7_L3LOG_SIZE / 4 * 3);
  3824. if (ret)
  3825. return ret;
  3826. /*
  3827. * Note: We do not worry about the concurrent register cacheline hang
  3828. * here because no other code should access these registers other than
  3829. * at initialization time.
  3830. */
  3831. for (i = 0; i < GEN7_L3LOG_SIZE; i += 4) {
  3832. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  3833. intel_ring_emit(ring, reg_base + i);
  3834. intel_ring_emit(ring, remap_info[i/4]);
  3835. }
  3836. intel_ring_advance(ring);
  3837. return ret;
  3838. }
  3839. void i915_gem_init_swizzling(struct drm_device *dev)
  3840. {
  3841. struct drm_i915_private *dev_priv = dev->dev_private;
  3842. if (INTEL_INFO(dev)->gen < 5 ||
  3843. dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE)
  3844. return;
  3845. I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
  3846. DISP_TILE_SURFACE_SWIZZLING);
  3847. if (IS_GEN5(dev))
  3848. return;
  3849. I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL);
  3850. if (IS_GEN6(dev))
  3851. I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_SNB));
  3852. else if (IS_GEN7(dev))
  3853. I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_IVB));
  3854. else if (IS_GEN8(dev))
  3855. I915_WRITE(GAMTARBMODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_BDW));
  3856. else
  3857. BUG();
  3858. }
  3859. static void init_unused_ring(struct drm_device *dev, u32 base)
  3860. {
  3861. struct drm_i915_private *dev_priv = dev->dev_private;
  3862. I915_WRITE(RING_CTL(base), 0);
  3863. I915_WRITE(RING_HEAD(base), 0);
  3864. I915_WRITE(RING_TAIL(base), 0);
  3865. I915_WRITE(RING_START(base), 0);
  3866. }
  3867. static void init_unused_rings(struct drm_device *dev)
  3868. {
  3869. if (IS_I830(dev)) {
  3870. init_unused_ring(dev, PRB1_BASE);
  3871. init_unused_ring(dev, SRB0_BASE);
  3872. init_unused_ring(dev, SRB1_BASE);
  3873. init_unused_ring(dev, SRB2_BASE);
  3874. init_unused_ring(dev, SRB3_BASE);
  3875. } else if (IS_GEN2(dev)) {
  3876. init_unused_ring(dev, SRB0_BASE);
  3877. init_unused_ring(dev, SRB1_BASE);
  3878. } else if (IS_GEN3(dev)) {
  3879. init_unused_ring(dev, PRB1_BASE);
  3880. init_unused_ring(dev, PRB2_BASE);
  3881. }
  3882. }
  3883. int i915_gem_init_rings(struct drm_device *dev)
  3884. {
  3885. struct drm_i915_private *dev_priv = dev->dev_private;
  3886. int ret;
  3887. ret = intel_init_render_ring_buffer(dev);
  3888. if (ret)
  3889. return ret;
  3890. if (HAS_BSD(dev)) {
  3891. ret = intel_init_bsd_ring_buffer(dev);
  3892. if (ret)
  3893. goto cleanup_render_ring;
  3894. }
  3895. if (HAS_BLT(dev)) {
  3896. ret = intel_init_blt_ring_buffer(dev);
  3897. if (ret)
  3898. goto cleanup_bsd_ring;
  3899. }
  3900. if (HAS_VEBOX(dev)) {
  3901. ret = intel_init_vebox_ring_buffer(dev);
  3902. if (ret)
  3903. goto cleanup_blt_ring;
  3904. }
  3905. if (HAS_BSD2(dev)) {
  3906. ret = intel_init_bsd2_ring_buffer(dev);
  3907. if (ret)
  3908. goto cleanup_vebox_ring;
  3909. }
  3910. return 0;
  3911. cleanup_vebox_ring:
  3912. intel_cleanup_ring_buffer(&dev_priv->ring[VECS]);
  3913. cleanup_blt_ring:
  3914. intel_cleanup_ring_buffer(&dev_priv->ring[BCS]);
  3915. cleanup_bsd_ring:
  3916. intel_cleanup_ring_buffer(&dev_priv->ring[VCS]);
  3917. cleanup_render_ring:
  3918. intel_cleanup_ring_buffer(&dev_priv->ring[RCS]);
  3919. return ret;
  3920. }
  3921. int
  3922. i915_gem_init_hw(struct drm_device *dev)
  3923. {
  3924. struct drm_i915_private *dev_priv = dev->dev_private;
  3925. struct intel_engine_cs *ring;
  3926. int ret, i, j;
  3927. if (INTEL_INFO(dev)->gen < 6 && !intel_enable_gtt())
  3928. return -EIO;
  3929. /* Double layer security blanket, see i915_gem_init() */
  3930. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  3931. if (dev_priv->ellc_size)
  3932. I915_WRITE(HSW_IDICR, I915_READ(HSW_IDICR) | IDIHASHMSK(0xf));
  3933. if (IS_HASWELL(dev))
  3934. I915_WRITE(MI_PREDICATE_RESULT_2, IS_HSW_GT3(dev) ?
  3935. LOWER_SLICE_ENABLED : LOWER_SLICE_DISABLED);
  3936. if (HAS_PCH_NOP(dev)) {
  3937. if (IS_IVYBRIDGE(dev)) {
  3938. u32 temp = I915_READ(GEN7_MSG_CTL);
  3939. temp &= ~(WAIT_FOR_PCH_FLR_ACK | WAIT_FOR_PCH_RESET_ACK);
  3940. I915_WRITE(GEN7_MSG_CTL, temp);
  3941. } else if (INTEL_INFO(dev)->gen >= 7) {
  3942. u32 temp = I915_READ(HSW_NDE_RSTWRN_OPT);
  3943. temp &= ~RESET_PCH_HANDSHAKE_ENABLE;
  3944. I915_WRITE(HSW_NDE_RSTWRN_OPT, temp);
  3945. }
  3946. }
  3947. i915_gem_init_swizzling(dev);
  3948. /*
  3949. * At least 830 can leave some of the unused rings
  3950. * "active" (ie. head != tail) after resume which
  3951. * will prevent c3 entry. Makes sure all unused rings
  3952. * are totally idle.
  3953. */
  3954. init_unused_rings(dev);
  3955. BUG_ON(!dev_priv->ring[RCS].default_context);
  3956. ret = i915_ppgtt_init_hw(dev);
  3957. if (ret) {
  3958. DRM_ERROR("PPGTT enable HW failed %d\n", ret);
  3959. goto out;
  3960. }
  3961. /* Need to do basic initialisation of all rings first: */
  3962. for_each_ring(ring, dev_priv, i) {
  3963. ret = ring->init_hw(ring);
  3964. if (ret)
  3965. goto out;
  3966. }
  3967. /* We can't enable contexts until all firmware is loaded */
  3968. if (HAS_GUC_UCODE(dev)) {
  3969. ret = intel_guc_ucode_load(dev);
  3970. if (ret) {
  3971. /*
  3972. * If we got an error and GuC submission is enabled, map
  3973. * the error to -EIO so the GPU will be declared wedged.
  3974. * OTOH, if we didn't intend to use the GuC anyway, just
  3975. * discard the error and carry on.
  3976. */
  3977. DRM_ERROR("Failed to initialize GuC, error %d%s\n", ret,
  3978. i915.enable_guc_submission ? "" :
  3979. " (ignored)");
  3980. ret = i915.enable_guc_submission ? -EIO : 0;
  3981. if (ret)
  3982. goto out;
  3983. }
  3984. }
  3985. /*
  3986. * Increment the next seqno by 0x100 so we have a visible break
  3987. * on re-initialisation
  3988. */
  3989. ret = i915_gem_set_seqno(dev, dev_priv->next_seqno+0x100);
  3990. if (ret)
  3991. goto out;
  3992. /* Now it is safe to go back round and do everything else: */
  3993. for_each_ring(ring, dev_priv, i) {
  3994. struct drm_i915_gem_request *req;
  3995. WARN_ON(!ring->default_context);
  3996. ret = i915_gem_request_alloc(ring, ring->default_context, &req);
  3997. if (ret) {
  3998. i915_gem_cleanup_ringbuffer(dev);
  3999. goto out;
  4000. }
  4001. if (ring->id == RCS) {
  4002. for (j = 0; j < NUM_L3_SLICES(dev); j++)
  4003. i915_gem_l3_remap(req, j);
  4004. }
  4005. ret = i915_ppgtt_init_ring(req);
  4006. if (ret && ret != -EIO) {
  4007. DRM_ERROR("PPGTT enable ring #%d failed %d\n", i, ret);
  4008. i915_gem_request_cancel(req);
  4009. i915_gem_cleanup_ringbuffer(dev);
  4010. goto out;
  4011. }
  4012. ret = i915_gem_context_enable(req);
  4013. if (ret && ret != -EIO) {
  4014. DRM_ERROR("Context enable ring #%d failed %d\n", i, ret);
  4015. i915_gem_request_cancel(req);
  4016. i915_gem_cleanup_ringbuffer(dev);
  4017. goto out;
  4018. }
  4019. i915_add_request_no_flush(req);
  4020. }
  4021. out:
  4022. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  4023. return ret;
  4024. }
  4025. int i915_gem_init(struct drm_device *dev)
  4026. {
  4027. struct drm_i915_private *dev_priv = dev->dev_private;
  4028. int ret;
  4029. i915.enable_execlists = intel_sanitize_enable_execlists(dev,
  4030. i915.enable_execlists);
  4031. mutex_lock(&dev->struct_mutex);
  4032. if (IS_VALLEYVIEW(dev)) {
  4033. /* VLVA0 (potential hack), BIOS isn't actually waking us */
  4034. I915_WRITE(VLV_GTLC_WAKE_CTRL, VLV_GTLC_ALLOWWAKEREQ);
  4035. if (wait_for((I915_READ(VLV_GTLC_PW_STATUS) &
  4036. VLV_GTLC_ALLOWWAKEACK), 10))
  4037. DRM_DEBUG_DRIVER("allow wake ack timed out\n");
  4038. }
  4039. if (!i915.enable_execlists) {
  4040. dev_priv->gt.execbuf_submit = i915_gem_ringbuffer_submission;
  4041. dev_priv->gt.init_rings = i915_gem_init_rings;
  4042. dev_priv->gt.cleanup_ring = intel_cleanup_ring_buffer;
  4043. dev_priv->gt.stop_ring = intel_stop_ring_buffer;
  4044. } else {
  4045. dev_priv->gt.execbuf_submit = intel_execlists_submission;
  4046. dev_priv->gt.init_rings = intel_logical_rings_init;
  4047. dev_priv->gt.cleanup_ring = intel_logical_ring_cleanup;
  4048. dev_priv->gt.stop_ring = intel_logical_ring_stop;
  4049. }
  4050. /* This is just a security blanket to placate dragons.
  4051. * On some systems, we very sporadically observe that the first TLBs
  4052. * used by the CS may be stale, despite us poking the TLB reset. If
  4053. * we hold the forcewake during initialisation these problems
  4054. * just magically go away.
  4055. */
  4056. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  4057. ret = i915_gem_init_userptr(dev);
  4058. if (ret)
  4059. goto out_unlock;
  4060. i915_gem_init_global_gtt(dev);
  4061. ret = i915_gem_context_init(dev);
  4062. if (ret)
  4063. goto out_unlock;
  4064. ret = dev_priv->gt.init_rings(dev);
  4065. if (ret)
  4066. goto out_unlock;
  4067. ret = i915_gem_init_hw(dev);
  4068. if (ret == -EIO) {
  4069. /* Allow ring initialisation to fail by marking the GPU as
  4070. * wedged. But we only want to do this where the GPU is angry,
  4071. * for all other failure, such as an allocation failure, bail.
  4072. */
  4073. DRM_ERROR("Failed to initialize GPU, declaring it wedged\n");
  4074. atomic_or(I915_WEDGED, &dev_priv->gpu_error.reset_counter);
  4075. ret = 0;
  4076. }
  4077. out_unlock:
  4078. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  4079. mutex_unlock(&dev->struct_mutex);
  4080. return ret;
  4081. }
  4082. void
  4083. i915_gem_cleanup_ringbuffer(struct drm_device *dev)
  4084. {
  4085. struct drm_i915_private *dev_priv = dev->dev_private;
  4086. struct intel_engine_cs *ring;
  4087. int i;
  4088. for_each_ring(ring, dev_priv, i)
  4089. dev_priv->gt.cleanup_ring(ring);
  4090. if (i915.enable_execlists)
  4091. /*
  4092. * Neither the BIOS, ourselves or any other kernel
  4093. * expects the system to be in execlists mode on startup,
  4094. * so we need to reset the GPU back to legacy mode.
  4095. */
  4096. intel_gpu_reset(dev);
  4097. }
  4098. static void
  4099. init_ring_lists(struct intel_engine_cs *ring)
  4100. {
  4101. INIT_LIST_HEAD(&ring->active_list);
  4102. INIT_LIST_HEAD(&ring->request_list);
  4103. }
  4104. void
  4105. i915_gem_load(struct drm_device *dev)
  4106. {
  4107. struct drm_i915_private *dev_priv = dev->dev_private;
  4108. int i;
  4109. dev_priv->objects =
  4110. kmem_cache_create("i915_gem_object",
  4111. sizeof(struct drm_i915_gem_object), 0,
  4112. SLAB_HWCACHE_ALIGN,
  4113. NULL);
  4114. dev_priv->vmas =
  4115. kmem_cache_create("i915_gem_vma",
  4116. sizeof(struct i915_vma), 0,
  4117. SLAB_HWCACHE_ALIGN,
  4118. NULL);
  4119. dev_priv->requests =
  4120. kmem_cache_create("i915_gem_request",
  4121. sizeof(struct drm_i915_gem_request), 0,
  4122. SLAB_HWCACHE_ALIGN,
  4123. NULL);
  4124. INIT_LIST_HEAD(&dev_priv->vm_list);
  4125. INIT_LIST_HEAD(&dev_priv->context_list);
  4126. INIT_LIST_HEAD(&dev_priv->mm.unbound_list);
  4127. INIT_LIST_HEAD(&dev_priv->mm.bound_list);
  4128. INIT_LIST_HEAD(&dev_priv->mm.fence_list);
  4129. for (i = 0; i < I915_NUM_RINGS; i++)
  4130. init_ring_lists(&dev_priv->ring[i]);
  4131. for (i = 0; i < I915_MAX_NUM_FENCES; i++)
  4132. INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list);
  4133. INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
  4134. i915_gem_retire_work_handler);
  4135. INIT_DELAYED_WORK(&dev_priv->mm.idle_work,
  4136. i915_gem_idle_work_handler);
  4137. init_waitqueue_head(&dev_priv->gpu_error.reset_queue);
  4138. dev_priv->relative_constants_mode = I915_EXEC_CONSTANTS_REL_GENERAL;
  4139. if (INTEL_INFO(dev)->gen >= 7 && !IS_VALLEYVIEW(dev))
  4140. dev_priv->num_fence_regs = 32;
  4141. else if (INTEL_INFO(dev)->gen >= 4 || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
  4142. dev_priv->num_fence_regs = 16;
  4143. else
  4144. dev_priv->num_fence_regs = 8;
  4145. if (intel_vgpu_active(dev))
  4146. dev_priv->num_fence_regs =
  4147. I915_READ(vgtif_reg(avail_rs.fence_num));
  4148. /*
  4149. * Set initial sequence number for requests.
  4150. * Using this number allows the wraparound to happen early,
  4151. * catching any obvious problems.
  4152. */
  4153. dev_priv->next_seqno = ((u32)~0 - 0x1100);
  4154. dev_priv->last_seqno = ((u32)~0 - 0x1101);
  4155. /* Initialize fence registers to zero */
  4156. INIT_LIST_HEAD(&dev_priv->mm.fence_list);
  4157. i915_gem_restore_fences(dev);
  4158. i915_gem_detect_bit_6_swizzle(dev);
  4159. init_waitqueue_head(&dev_priv->pending_flip_queue);
  4160. dev_priv->mm.interruptible = true;
  4161. i915_gem_shrinker_init(dev_priv);
  4162. mutex_init(&dev_priv->fb_tracking.lock);
  4163. }
  4164. void i915_gem_release(struct drm_device *dev, struct drm_file *file)
  4165. {
  4166. struct drm_i915_file_private *file_priv = file->driver_priv;
  4167. /* Clean up our request list when the client is going away, so that
  4168. * later retire_requests won't dereference our soon-to-be-gone
  4169. * file_priv.
  4170. */
  4171. spin_lock(&file_priv->mm.lock);
  4172. while (!list_empty(&file_priv->mm.request_list)) {
  4173. struct drm_i915_gem_request *request;
  4174. request = list_first_entry(&file_priv->mm.request_list,
  4175. struct drm_i915_gem_request,
  4176. client_list);
  4177. list_del(&request->client_list);
  4178. request->file_priv = NULL;
  4179. }
  4180. spin_unlock(&file_priv->mm.lock);
  4181. if (!list_empty(&file_priv->rps.link)) {
  4182. spin_lock(&to_i915(dev)->rps.client_lock);
  4183. list_del(&file_priv->rps.link);
  4184. spin_unlock(&to_i915(dev)->rps.client_lock);
  4185. }
  4186. }
  4187. int i915_gem_open(struct drm_device *dev, struct drm_file *file)
  4188. {
  4189. struct drm_i915_file_private *file_priv;
  4190. int ret;
  4191. DRM_DEBUG_DRIVER("\n");
  4192. file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL);
  4193. if (!file_priv)
  4194. return -ENOMEM;
  4195. file->driver_priv = file_priv;
  4196. file_priv->dev_priv = dev->dev_private;
  4197. file_priv->file = file;
  4198. INIT_LIST_HEAD(&file_priv->rps.link);
  4199. spin_lock_init(&file_priv->mm.lock);
  4200. INIT_LIST_HEAD(&file_priv->mm.request_list);
  4201. ret = i915_gem_context_open(dev, file);
  4202. if (ret)
  4203. kfree(file_priv);
  4204. return ret;
  4205. }
  4206. /**
  4207. * i915_gem_track_fb - update frontbuffer tracking
  4208. * @old: current GEM buffer for the frontbuffer slots
  4209. * @new: new GEM buffer for the frontbuffer slots
  4210. * @frontbuffer_bits: bitmask of frontbuffer slots
  4211. *
  4212. * This updates the frontbuffer tracking bits @frontbuffer_bits by clearing them
  4213. * from @old and setting them in @new. Both @old and @new can be NULL.
  4214. */
  4215. void i915_gem_track_fb(struct drm_i915_gem_object *old,
  4216. struct drm_i915_gem_object *new,
  4217. unsigned frontbuffer_bits)
  4218. {
  4219. if (old) {
  4220. WARN_ON(!mutex_is_locked(&old->base.dev->struct_mutex));
  4221. WARN_ON(!(old->frontbuffer_bits & frontbuffer_bits));
  4222. old->frontbuffer_bits &= ~frontbuffer_bits;
  4223. }
  4224. if (new) {
  4225. WARN_ON(!mutex_is_locked(&new->base.dev->struct_mutex));
  4226. WARN_ON(new->frontbuffer_bits & frontbuffer_bits);
  4227. new->frontbuffer_bits |= frontbuffer_bits;
  4228. }
  4229. }
  4230. /* All the new VM stuff */
  4231. u64 i915_gem_obj_offset(struct drm_i915_gem_object *o,
  4232. struct i915_address_space *vm)
  4233. {
  4234. struct drm_i915_private *dev_priv = o->base.dev->dev_private;
  4235. struct i915_vma *vma;
  4236. WARN_ON(vm == &dev_priv->mm.aliasing_ppgtt->base);
  4237. list_for_each_entry(vma, &o->vma_list, vma_link) {
  4238. if (i915_is_ggtt(vma->vm) &&
  4239. vma->ggtt_view.type != I915_GGTT_VIEW_NORMAL)
  4240. continue;
  4241. if (vma->vm == vm)
  4242. return vma->node.start;
  4243. }
  4244. WARN(1, "%s vma for this object not found.\n",
  4245. i915_is_ggtt(vm) ? "global" : "ppgtt");
  4246. return -1;
  4247. }
  4248. u64 i915_gem_obj_ggtt_offset_view(struct drm_i915_gem_object *o,
  4249. const struct i915_ggtt_view *view)
  4250. {
  4251. struct i915_address_space *ggtt = i915_obj_to_ggtt(o);
  4252. struct i915_vma *vma;
  4253. list_for_each_entry(vma, &o->vma_list, vma_link)
  4254. if (vma->vm == ggtt &&
  4255. i915_ggtt_view_equal(&vma->ggtt_view, view))
  4256. return vma->node.start;
  4257. WARN(1, "global vma for this object not found. (view=%u)\n", view->type);
  4258. return -1;
  4259. }
  4260. bool i915_gem_obj_bound(struct drm_i915_gem_object *o,
  4261. struct i915_address_space *vm)
  4262. {
  4263. struct i915_vma *vma;
  4264. list_for_each_entry(vma, &o->vma_list, vma_link) {
  4265. if (i915_is_ggtt(vma->vm) &&
  4266. vma->ggtt_view.type != I915_GGTT_VIEW_NORMAL)
  4267. continue;
  4268. if (vma->vm == vm && drm_mm_node_allocated(&vma->node))
  4269. return true;
  4270. }
  4271. return false;
  4272. }
  4273. bool i915_gem_obj_ggtt_bound_view(struct drm_i915_gem_object *o,
  4274. const struct i915_ggtt_view *view)
  4275. {
  4276. struct i915_address_space *ggtt = i915_obj_to_ggtt(o);
  4277. struct i915_vma *vma;
  4278. list_for_each_entry(vma, &o->vma_list, vma_link)
  4279. if (vma->vm == ggtt &&
  4280. i915_ggtt_view_equal(&vma->ggtt_view, view) &&
  4281. drm_mm_node_allocated(&vma->node))
  4282. return true;
  4283. return false;
  4284. }
  4285. bool i915_gem_obj_bound_any(struct drm_i915_gem_object *o)
  4286. {
  4287. struct i915_vma *vma;
  4288. list_for_each_entry(vma, &o->vma_list, vma_link)
  4289. if (drm_mm_node_allocated(&vma->node))
  4290. return true;
  4291. return false;
  4292. }
  4293. unsigned long i915_gem_obj_size(struct drm_i915_gem_object *o,
  4294. struct i915_address_space *vm)
  4295. {
  4296. struct drm_i915_private *dev_priv = o->base.dev->dev_private;
  4297. struct i915_vma *vma;
  4298. WARN_ON(vm == &dev_priv->mm.aliasing_ppgtt->base);
  4299. BUG_ON(list_empty(&o->vma_list));
  4300. list_for_each_entry(vma, &o->vma_list, vma_link) {
  4301. if (i915_is_ggtt(vma->vm) &&
  4302. vma->ggtt_view.type != I915_GGTT_VIEW_NORMAL)
  4303. continue;
  4304. if (vma->vm == vm)
  4305. return vma->node.size;
  4306. }
  4307. return 0;
  4308. }
  4309. bool i915_gem_obj_is_pinned(struct drm_i915_gem_object *obj)
  4310. {
  4311. struct i915_vma *vma;
  4312. list_for_each_entry(vma, &obj->vma_list, vma_link)
  4313. if (vma->pin_count > 0)
  4314. return true;
  4315. return false;
  4316. }
  4317. /* Allocate a new GEM object and fill it with the supplied data */
  4318. struct drm_i915_gem_object *
  4319. i915_gem_object_create_from_data(struct drm_device *dev,
  4320. const void *data, size_t size)
  4321. {
  4322. struct drm_i915_gem_object *obj;
  4323. struct sg_table *sg;
  4324. size_t bytes;
  4325. int ret;
  4326. obj = i915_gem_alloc_object(dev, round_up(size, PAGE_SIZE));
  4327. if (IS_ERR_OR_NULL(obj))
  4328. return obj;
  4329. ret = i915_gem_object_set_to_cpu_domain(obj, true);
  4330. if (ret)
  4331. goto fail;
  4332. ret = i915_gem_object_get_pages(obj);
  4333. if (ret)
  4334. goto fail;
  4335. i915_gem_object_pin_pages(obj);
  4336. sg = obj->pages;
  4337. bytes = sg_copy_from_buffer(sg->sgl, sg->nents, (void *)data, size);
  4338. i915_gem_object_unpin_pages(obj);
  4339. if (WARN_ON(bytes != size)) {
  4340. DRM_ERROR("Incomplete copy, wrote %zu of %zu", bytes, size);
  4341. ret = -EFAULT;
  4342. goto fail;
  4343. }
  4344. return obj;
  4345. fail:
  4346. drm_gem_object_unreference(&obj->base);
  4347. return ERR_PTR(ret);
  4348. }