i915_gem.c 127 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798
  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_gem_dmabuf.h"
  32. #include "i915_vgpu.h"
  33. #include "i915_trace.h"
  34. #include "intel_drv.h"
  35. #include "intel_frontbuffer.h"
  36. #include "intel_mocs.h"
  37. #include <linux/reservation.h>
  38. #include <linux/shmem_fs.h>
  39. #include <linux/slab.h>
  40. #include <linux/swap.h>
  41. #include <linux/pci.h>
  42. #include <linux/dma-buf.h>
  43. static void i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj);
  44. static void i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj);
  45. static bool cpu_cache_is_coherent(struct drm_device *dev,
  46. enum i915_cache_level level)
  47. {
  48. return HAS_LLC(dev) || level != I915_CACHE_NONE;
  49. }
  50. static bool cpu_write_needs_clflush(struct drm_i915_gem_object *obj)
  51. {
  52. if (obj->base.write_domain == I915_GEM_DOMAIN_CPU)
  53. return false;
  54. if (!cpu_cache_is_coherent(obj->base.dev, obj->cache_level))
  55. return true;
  56. return obj->pin_display;
  57. }
  58. static int
  59. insert_mappable_node(struct drm_i915_private *i915,
  60. struct drm_mm_node *node, u32 size)
  61. {
  62. memset(node, 0, sizeof(*node));
  63. return drm_mm_insert_node_in_range_generic(&i915->ggtt.base.mm, node,
  64. size, 0, 0, 0,
  65. i915->ggtt.mappable_end,
  66. DRM_MM_SEARCH_DEFAULT,
  67. DRM_MM_CREATE_DEFAULT);
  68. }
  69. static void
  70. remove_mappable_node(struct drm_mm_node *node)
  71. {
  72. drm_mm_remove_node(node);
  73. }
  74. /* some bookkeeping */
  75. static void i915_gem_info_add_obj(struct drm_i915_private *dev_priv,
  76. size_t size)
  77. {
  78. spin_lock(&dev_priv->mm.object_stat_lock);
  79. dev_priv->mm.object_count++;
  80. dev_priv->mm.object_memory += size;
  81. spin_unlock(&dev_priv->mm.object_stat_lock);
  82. }
  83. static void i915_gem_info_remove_obj(struct drm_i915_private *dev_priv,
  84. size_t size)
  85. {
  86. spin_lock(&dev_priv->mm.object_stat_lock);
  87. dev_priv->mm.object_count--;
  88. dev_priv->mm.object_memory -= size;
  89. spin_unlock(&dev_priv->mm.object_stat_lock);
  90. }
  91. static int
  92. i915_gem_wait_for_error(struct i915_gpu_error *error)
  93. {
  94. int ret;
  95. if (!i915_reset_in_progress(error))
  96. return 0;
  97. /*
  98. * Only wait 10 seconds for the gpu reset to complete to avoid hanging
  99. * userspace. If it takes that long something really bad is going on and
  100. * we should simply try to bail out and fail as gracefully as possible.
  101. */
  102. ret = wait_event_interruptible_timeout(error->reset_queue,
  103. !i915_reset_in_progress(error),
  104. 10*HZ);
  105. if (ret == 0) {
  106. DRM_ERROR("Timed out waiting for the gpu reset to complete\n");
  107. return -EIO;
  108. } else if (ret < 0) {
  109. return ret;
  110. } else {
  111. return 0;
  112. }
  113. }
  114. int i915_mutex_lock_interruptible(struct drm_device *dev)
  115. {
  116. struct drm_i915_private *dev_priv = to_i915(dev);
  117. int ret;
  118. ret = i915_gem_wait_for_error(&dev_priv->gpu_error);
  119. if (ret)
  120. return ret;
  121. ret = mutex_lock_interruptible(&dev->struct_mutex);
  122. if (ret)
  123. return ret;
  124. return 0;
  125. }
  126. int
  127. i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
  128. struct drm_file *file)
  129. {
  130. struct drm_i915_private *dev_priv = to_i915(dev);
  131. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  132. struct drm_i915_gem_get_aperture *args = data;
  133. struct i915_vma *vma;
  134. size_t pinned;
  135. pinned = 0;
  136. mutex_lock(&dev->struct_mutex);
  137. list_for_each_entry(vma, &ggtt->base.active_list, vm_link)
  138. if (i915_vma_is_pinned(vma))
  139. pinned += vma->node.size;
  140. list_for_each_entry(vma, &ggtt->base.inactive_list, vm_link)
  141. if (i915_vma_is_pinned(vma))
  142. pinned += vma->node.size;
  143. mutex_unlock(&dev->struct_mutex);
  144. args->aper_size = ggtt->base.total;
  145. args->aper_available_size = args->aper_size - pinned;
  146. return 0;
  147. }
  148. static int
  149. i915_gem_object_get_pages_phys(struct drm_i915_gem_object *obj)
  150. {
  151. struct address_space *mapping = obj->base.filp->f_mapping;
  152. char *vaddr = obj->phys_handle->vaddr;
  153. struct sg_table *st;
  154. struct scatterlist *sg;
  155. int i;
  156. if (WARN_ON(i915_gem_object_needs_bit17_swizzle(obj)))
  157. return -EINVAL;
  158. for (i = 0; i < obj->base.size / PAGE_SIZE; i++) {
  159. struct page *page;
  160. char *src;
  161. page = shmem_read_mapping_page(mapping, i);
  162. if (IS_ERR(page))
  163. return PTR_ERR(page);
  164. src = kmap_atomic(page);
  165. memcpy(vaddr, src, PAGE_SIZE);
  166. drm_clflush_virt_range(vaddr, PAGE_SIZE);
  167. kunmap_atomic(src);
  168. put_page(page);
  169. vaddr += PAGE_SIZE;
  170. }
  171. i915_gem_chipset_flush(to_i915(obj->base.dev));
  172. st = kmalloc(sizeof(*st), GFP_KERNEL);
  173. if (st == NULL)
  174. return -ENOMEM;
  175. if (sg_alloc_table(st, 1, GFP_KERNEL)) {
  176. kfree(st);
  177. return -ENOMEM;
  178. }
  179. sg = st->sgl;
  180. sg->offset = 0;
  181. sg->length = obj->base.size;
  182. sg_dma_address(sg) = obj->phys_handle->busaddr;
  183. sg_dma_len(sg) = obj->base.size;
  184. obj->pages = st;
  185. return 0;
  186. }
  187. static void
  188. i915_gem_object_put_pages_phys(struct drm_i915_gem_object *obj)
  189. {
  190. int ret;
  191. BUG_ON(obj->madv == __I915_MADV_PURGED);
  192. ret = i915_gem_object_set_to_cpu_domain(obj, true);
  193. if (WARN_ON(ret)) {
  194. /* In the event of a disaster, abandon all caches and
  195. * hope for the best.
  196. */
  197. obj->base.read_domains = obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  198. }
  199. if (obj->madv == I915_MADV_DONTNEED)
  200. obj->dirty = 0;
  201. if (obj->dirty) {
  202. struct address_space *mapping = obj->base.filp->f_mapping;
  203. char *vaddr = obj->phys_handle->vaddr;
  204. int i;
  205. for (i = 0; i < obj->base.size / PAGE_SIZE; i++) {
  206. struct page *page;
  207. char *dst;
  208. page = shmem_read_mapping_page(mapping, i);
  209. if (IS_ERR(page))
  210. continue;
  211. dst = kmap_atomic(page);
  212. drm_clflush_virt_range(vaddr, PAGE_SIZE);
  213. memcpy(dst, vaddr, PAGE_SIZE);
  214. kunmap_atomic(dst);
  215. set_page_dirty(page);
  216. if (obj->madv == I915_MADV_WILLNEED)
  217. mark_page_accessed(page);
  218. put_page(page);
  219. vaddr += PAGE_SIZE;
  220. }
  221. obj->dirty = 0;
  222. }
  223. sg_free_table(obj->pages);
  224. kfree(obj->pages);
  225. }
  226. static void
  227. i915_gem_object_release_phys(struct drm_i915_gem_object *obj)
  228. {
  229. drm_pci_free(obj->base.dev, obj->phys_handle);
  230. }
  231. static const struct drm_i915_gem_object_ops i915_gem_phys_ops = {
  232. .get_pages = i915_gem_object_get_pages_phys,
  233. .put_pages = i915_gem_object_put_pages_phys,
  234. .release = i915_gem_object_release_phys,
  235. };
  236. int i915_gem_object_unbind(struct drm_i915_gem_object *obj)
  237. {
  238. struct i915_vma *vma;
  239. LIST_HEAD(still_in_list);
  240. int ret;
  241. lockdep_assert_held(&obj->base.dev->struct_mutex);
  242. /* Closed vma are removed from the obj->vma_list - but they may
  243. * still have an active binding on the object. To remove those we
  244. * must wait for all rendering to complete to the object (as unbinding
  245. * must anyway), and retire the requests.
  246. */
  247. ret = i915_gem_object_wait_rendering(obj, false);
  248. if (ret)
  249. return ret;
  250. i915_gem_retire_requests(to_i915(obj->base.dev));
  251. while ((vma = list_first_entry_or_null(&obj->vma_list,
  252. struct i915_vma,
  253. obj_link))) {
  254. list_move_tail(&vma->obj_link, &still_in_list);
  255. ret = i915_vma_unbind(vma);
  256. if (ret)
  257. break;
  258. }
  259. list_splice(&still_in_list, &obj->vma_list);
  260. return ret;
  261. }
  262. /**
  263. * Ensures that all rendering to the object has completed and the object is
  264. * safe to unbind from the GTT or access from the CPU.
  265. * @obj: i915 gem object
  266. * @readonly: waiting for just read access or read-write access
  267. */
  268. int
  269. i915_gem_object_wait_rendering(struct drm_i915_gem_object *obj,
  270. bool readonly)
  271. {
  272. struct reservation_object *resv;
  273. struct i915_gem_active *active;
  274. unsigned long active_mask;
  275. int idx;
  276. lockdep_assert_held(&obj->base.dev->struct_mutex);
  277. if (!readonly) {
  278. active = obj->last_read;
  279. active_mask = i915_gem_object_get_active(obj);
  280. } else {
  281. active_mask = 1;
  282. active = &obj->last_write;
  283. }
  284. for_each_active(active_mask, idx) {
  285. int ret;
  286. ret = i915_gem_active_wait(&active[idx],
  287. &obj->base.dev->struct_mutex);
  288. if (ret)
  289. return ret;
  290. }
  291. resv = i915_gem_object_get_dmabuf_resv(obj);
  292. if (resv) {
  293. long err;
  294. err = reservation_object_wait_timeout_rcu(resv, !readonly, true,
  295. MAX_SCHEDULE_TIMEOUT);
  296. if (err < 0)
  297. return err;
  298. }
  299. return 0;
  300. }
  301. /* A nonblocking variant of the above wait. Must be called prior to
  302. * acquiring the mutex for the object, as the object state may change
  303. * during this call. A reference must be held by the caller for the object.
  304. */
  305. static __must_check int
  306. __unsafe_wait_rendering(struct drm_i915_gem_object *obj,
  307. struct intel_rps_client *rps,
  308. bool readonly)
  309. {
  310. struct i915_gem_active *active;
  311. unsigned long active_mask;
  312. int idx;
  313. active_mask = __I915_BO_ACTIVE(obj);
  314. if (!active_mask)
  315. return 0;
  316. if (!readonly) {
  317. active = obj->last_read;
  318. } else {
  319. active_mask = 1;
  320. active = &obj->last_write;
  321. }
  322. for_each_active(active_mask, idx) {
  323. int ret;
  324. ret = i915_gem_active_wait_unlocked(&active[idx],
  325. I915_WAIT_INTERRUPTIBLE,
  326. NULL, rps);
  327. if (ret)
  328. return ret;
  329. }
  330. return 0;
  331. }
  332. static struct intel_rps_client *to_rps_client(struct drm_file *file)
  333. {
  334. struct drm_i915_file_private *fpriv = file->driver_priv;
  335. return &fpriv->rps;
  336. }
  337. int
  338. i915_gem_object_attach_phys(struct drm_i915_gem_object *obj,
  339. int align)
  340. {
  341. drm_dma_handle_t *phys;
  342. int ret;
  343. if (obj->phys_handle) {
  344. if ((unsigned long)obj->phys_handle->vaddr & (align -1))
  345. return -EBUSY;
  346. return 0;
  347. }
  348. if (obj->madv != I915_MADV_WILLNEED)
  349. return -EFAULT;
  350. if (obj->base.filp == NULL)
  351. return -EINVAL;
  352. ret = i915_gem_object_unbind(obj);
  353. if (ret)
  354. return ret;
  355. ret = i915_gem_object_put_pages(obj);
  356. if (ret)
  357. return ret;
  358. /* create a new object */
  359. phys = drm_pci_alloc(obj->base.dev, obj->base.size, align);
  360. if (!phys)
  361. return -ENOMEM;
  362. obj->phys_handle = phys;
  363. obj->ops = &i915_gem_phys_ops;
  364. return i915_gem_object_get_pages(obj);
  365. }
  366. static int
  367. i915_gem_phys_pwrite(struct drm_i915_gem_object *obj,
  368. struct drm_i915_gem_pwrite *args,
  369. struct drm_file *file_priv)
  370. {
  371. struct drm_device *dev = obj->base.dev;
  372. void *vaddr = obj->phys_handle->vaddr + args->offset;
  373. char __user *user_data = u64_to_user_ptr(args->data_ptr);
  374. int ret = 0;
  375. /* We manually control the domain here and pretend that it
  376. * remains coherent i.e. in the GTT domain, like shmem_pwrite.
  377. */
  378. ret = i915_gem_object_wait_rendering(obj, false);
  379. if (ret)
  380. return ret;
  381. intel_fb_obj_invalidate(obj, ORIGIN_CPU);
  382. if (__copy_from_user_inatomic_nocache(vaddr, user_data, args->size)) {
  383. unsigned long unwritten;
  384. /* The physical object once assigned is fixed for the lifetime
  385. * of the obj, so we can safely drop the lock and continue
  386. * to access vaddr.
  387. */
  388. mutex_unlock(&dev->struct_mutex);
  389. unwritten = copy_from_user(vaddr, user_data, args->size);
  390. mutex_lock(&dev->struct_mutex);
  391. if (unwritten) {
  392. ret = -EFAULT;
  393. goto out;
  394. }
  395. }
  396. drm_clflush_virt_range(vaddr, args->size);
  397. i915_gem_chipset_flush(to_i915(dev));
  398. out:
  399. intel_fb_obj_flush(obj, false, ORIGIN_CPU);
  400. return ret;
  401. }
  402. void *i915_gem_object_alloc(struct drm_device *dev)
  403. {
  404. struct drm_i915_private *dev_priv = to_i915(dev);
  405. return kmem_cache_zalloc(dev_priv->objects, GFP_KERNEL);
  406. }
  407. void i915_gem_object_free(struct drm_i915_gem_object *obj)
  408. {
  409. struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
  410. kmem_cache_free(dev_priv->objects, obj);
  411. }
  412. static int
  413. i915_gem_create(struct drm_file *file,
  414. struct drm_device *dev,
  415. uint64_t size,
  416. uint32_t *handle_p)
  417. {
  418. struct drm_i915_gem_object *obj;
  419. int ret;
  420. u32 handle;
  421. size = roundup(size, PAGE_SIZE);
  422. if (size == 0)
  423. return -EINVAL;
  424. /* Allocate the new object */
  425. obj = i915_gem_object_create(dev, size);
  426. if (IS_ERR(obj))
  427. return PTR_ERR(obj);
  428. ret = drm_gem_handle_create(file, &obj->base, &handle);
  429. /* drop reference from allocate - handle holds it now */
  430. i915_gem_object_put_unlocked(obj);
  431. if (ret)
  432. return ret;
  433. *handle_p = handle;
  434. return 0;
  435. }
  436. int
  437. i915_gem_dumb_create(struct drm_file *file,
  438. struct drm_device *dev,
  439. struct drm_mode_create_dumb *args)
  440. {
  441. /* have to work out size/pitch and return them */
  442. args->pitch = ALIGN(args->width * DIV_ROUND_UP(args->bpp, 8), 64);
  443. args->size = args->pitch * args->height;
  444. return i915_gem_create(file, dev,
  445. args->size, &args->handle);
  446. }
  447. /**
  448. * Creates a new mm object and returns a handle to it.
  449. * @dev: drm device pointer
  450. * @data: ioctl data blob
  451. * @file: drm file pointer
  452. */
  453. int
  454. i915_gem_create_ioctl(struct drm_device *dev, void *data,
  455. struct drm_file *file)
  456. {
  457. struct drm_i915_gem_create *args = data;
  458. return i915_gem_create(file, dev,
  459. args->size, &args->handle);
  460. }
  461. static inline int
  462. __copy_to_user_swizzled(char __user *cpu_vaddr,
  463. const char *gpu_vaddr, int gpu_offset,
  464. int length)
  465. {
  466. int ret, cpu_offset = 0;
  467. while (length > 0) {
  468. int cacheline_end = ALIGN(gpu_offset + 1, 64);
  469. int this_length = min(cacheline_end - gpu_offset, length);
  470. int swizzled_gpu_offset = gpu_offset ^ 64;
  471. ret = __copy_to_user(cpu_vaddr + cpu_offset,
  472. gpu_vaddr + swizzled_gpu_offset,
  473. this_length);
  474. if (ret)
  475. return ret + length;
  476. cpu_offset += this_length;
  477. gpu_offset += this_length;
  478. length -= this_length;
  479. }
  480. return 0;
  481. }
  482. static inline int
  483. __copy_from_user_swizzled(char *gpu_vaddr, int gpu_offset,
  484. const char __user *cpu_vaddr,
  485. int length)
  486. {
  487. int ret, cpu_offset = 0;
  488. while (length > 0) {
  489. int cacheline_end = ALIGN(gpu_offset + 1, 64);
  490. int this_length = min(cacheline_end - gpu_offset, length);
  491. int swizzled_gpu_offset = gpu_offset ^ 64;
  492. ret = __copy_from_user(gpu_vaddr + swizzled_gpu_offset,
  493. cpu_vaddr + cpu_offset,
  494. this_length);
  495. if (ret)
  496. return ret + length;
  497. cpu_offset += this_length;
  498. gpu_offset += this_length;
  499. length -= this_length;
  500. }
  501. return 0;
  502. }
  503. /*
  504. * Pins the specified object's pages and synchronizes the object with
  505. * GPU accesses. Sets needs_clflush to non-zero if the caller should
  506. * flush the object from the CPU cache.
  507. */
  508. int i915_gem_obj_prepare_shmem_read(struct drm_i915_gem_object *obj,
  509. unsigned int *needs_clflush)
  510. {
  511. int ret;
  512. *needs_clflush = 0;
  513. if (!i915_gem_object_has_struct_page(obj))
  514. return -ENODEV;
  515. ret = i915_gem_object_wait_rendering(obj, true);
  516. if (ret)
  517. return ret;
  518. ret = i915_gem_object_get_pages(obj);
  519. if (ret)
  520. return ret;
  521. i915_gem_object_pin_pages(obj);
  522. i915_gem_object_flush_gtt_write_domain(obj);
  523. /* If we're not in the cpu read domain, set ourself into the gtt
  524. * read domain and manually flush cachelines (if required). This
  525. * optimizes for the case when the gpu will dirty the data
  526. * anyway again before the next pread happens.
  527. */
  528. if (!(obj->base.read_domains & I915_GEM_DOMAIN_CPU))
  529. *needs_clflush = !cpu_cache_is_coherent(obj->base.dev,
  530. obj->cache_level);
  531. if (*needs_clflush && !static_cpu_has(X86_FEATURE_CLFLUSH)) {
  532. ret = i915_gem_object_set_to_cpu_domain(obj, false);
  533. if (ret)
  534. goto err_unpin;
  535. *needs_clflush = 0;
  536. }
  537. /* return with the pages pinned */
  538. return 0;
  539. err_unpin:
  540. i915_gem_object_unpin_pages(obj);
  541. return ret;
  542. }
  543. int i915_gem_obj_prepare_shmem_write(struct drm_i915_gem_object *obj,
  544. unsigned int *needs_clflush)
  545. {
  546. int ret;
  547. *needs_clflush = 0;
  548. if (!i915_gem_object_has_struct_page(obj))
  549. return -ENODEV;
  550. ret = i915_gem_object_wait_rendering(obj, false);
  551. if (ret)
  552. return ret;
  553. ret = i915_gem_object_get_pages(obj);
  554. if (ret)
  555. return ret;
  556. i915_gem_object_pin_pages(obj);
  557. i915_gem_object_flush_gtt_write_domain(obj);
  558. /* If we're not in the cpu write domain, set ourself into the
  559. * gtt write domain and manually flush cachelines (as required).
  560. * This optimizes for the case when the gpu will use the data
  561. * right away and we therefore have to clflush anyway.
  562. */
  563. if (obj->base.write_domain != I915_GEM_DOMAIN_CPU)
  564. *needs_clflush |= cpu_write_needs_clflush(obj) << 1;
  565. /* Same trick applies to invalidate partially written cachelines read
  566. * before writing.
  567. */
  568. if (!(obj->base.read_domains & I915_GEM_DOMAIN_CPU))
  569. *needs_clflush |= !cpu_cache_is_coherent(obj->base.dev,
  570. obj->cache_level);
  571. if (*needs_clflush && !static_cpu_has(X86_FEATURE_CLFLUSH)) {
  572. ret = i915_gem_object_set_to_cpu_domain(obj, true);
  573. if (ret)
  574. goto err_unpin;
  575. *needs_clflush = 0;
  576. }
  577. if ((*needs_clflush & CLFLUSH_AFTER) == 0)
  578. obj->cache_dirty = true;
  579. intel_fb_obj_invalidate(obj, ORIGIN_CPU);
  580. obj->dirty = 1;
  581. /* return with the pages pinned */
  582. return 0;
  583. err_unpin:
  584. i915_gem_object_unpin_pages(obj);
  585. return ret;
  586. }
  587. /* Per-page copy function for the shmem pread fastpath.
  588. * Flushes invalid cachelines before reading the target if
  589. * needs_clflush is set. */
  590. static int
  591. shmem_pread_fast(struct page *page, int shmem_page_offset, int page_length,
  592. char __user *user_data,
  593. bool page_do_bit17_swizzling, bool needs_clflush)
  594. {
  595. char *vaddr;
  596. int ret;
  597. if (unlikely(page_do_bit17_swizzling))
  598. return -EINVAL;
  599. vaddr = kmap_atomic(page);
  600. if (needs_clflush)
  601. drm_clflush_virt_range(vaddr + shmem_page_offset,
  602. page_length);
  603. ret = __copy_to_user_inatomic(user_data,
  604. vaddr + shmem_page_offset,
  605. page_length);
  606. kunmap_atomic(vaddr);
  607. return ret ? -EFAULT : 0;
  608. }
  609. static void
  610. shmem_clflush_swizzled_range(char *addr, unsigned long length,
  611. bool swizzled)
  612. {
  613. if (unlikely(swizzled)) {
  614. unsigned long start = (unsigned long) addr;
  615. unsigned long end = (unsigned long) addr + length;
  616. /* For swizzling simply ensure that we always flush both
  617. * channels. Lame, but simple and it works. Swizzled
  618. * pwrite/pread is far from a hotpath - current userspace
  619. * doesn't use it at all. */
  620. start = round_down(start, 128);
  621. end = round_up(end, 128);
  622. drm_clflush_virt_range((void *)start, end - start);
  623. } else {
  624. drm_clflush_virt_range(addr, length);
  625. }
  626. }
  627. /* Only difference to the fast-path function is that this can handle bit17
  628. * and uses non-atomic copy and kmap functions. */
  629. static int
  630. shmem_pread_slow(struct page *page, int shmem_page_offset, int page_length,
  631. char __user *user_data,
  632. bool page_do_bit17_swizzling, bool needs_clflush)
  633. {
  634. char *vaddr;
  635. int ret;
  636. vaddr = kmap(page);
  637. if (needs_clflush)
  638. shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
  639. page_length,
  640. page_do_bit17_swizzling);
  641. if (page_do_bit17_swizzling)
  642. ret = __copy_to_user_swizzled(user_data,
  643. vaddr, shmem_page_offset,
  644. page_length);
  645. else
  646. ret = __copy_to_user(user_data,
  647. vaddr + shmem_page_offset,
  648. page_length);
  649. kunmap(page);
  650. return ret ? - EFAULT : 0;
  651. }
  652. static inline unsigned long
  653. slow_user_access(struct io_mapping *mapping,
  654. uint64_t page_base, int page_offset,
  655. char __user *user_data,
  656. unsigned long length, bool pwrite)
  657. {
  658. void __iomem *ioaddr;
  659. void *vaddr;
  660. uint64_t unwritten;
  661. ioaddr = io_mapping_map_wc(mapping, page_base, PAGE_SIZE);
  662. /* We can use the cpu mem copy function because this is X86. */
  663. vaddr = (void __force *)ioaddr + page_offset;
  664. if (pwrite)
  665. unwritten = __copy_from_user(vaddr, user_data, length);
  666. else
  667. unwritten = __copy_to_user(user_data, vaddr, length);
  668. io_mapping_unmap(ioaddr);
  669. return unwritten;
  670. }
  671. static int
  672. i915_gem_gtt_pread(struct drm_device *dev,
  673. struct drm_i915_gem_object *obj, uint64_t size,
  674. uint64_t data_offset, uint64_t data_ptr)
  675. {
  676. struct drm_i915_private *dev_priv = to_i915(dev);
  677. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  678. struct i915_vma *vma;
  679. struct drm_mm_node node;
  680. char __user *user_data;
  681. uint64_t remain;
  682. uint64_t offset;
  683. int ret;
  684. vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, PIN_MAPPABLE);
  685. if (!IS_ERR(vma)) {
  686. node.start = i915_ggtt_offset(vma);
  687. node.allocated = false;
  688. ret = i915_vma_put_fence(vma);
  689. if (ret) {
  690. i915_vma_unpin(vma);
  691. vma = ERR_PTR(ret);
  692. }
  693. }
  694. if (IS_ERR(vma)) {
  695. ret = insert_mappable_node(dev_priv, &node, PAGE_SIZE);
  696. if (ret)
  697. goto out;
  698. ret = i915_gem_object_get_pages(obj);
  699. if (ret) {
  700. remove_mappable_node(&node);
  701. goto out;
  702. }
  703. i915_gem_object_pin_pages(obj);
  704. }
  705. ret = i915_gem_object_set_to_gtt_domain(obj, false);
  706. if (ret)
  707. goto out_unpin;
  708. user_data = u64_to_user_ptr(data_ptr);
  709. remain = size;
  710. offset = data_offset;
  711. mutex_unlock(&dev->struct_mutex);
  712. if (likely(!i915.prefault_disable)) {
  713. ret = fault_in_pages_writeable(user_data, remain);
  714. if (ret) {
  715. mutex_lock(&dev->struct_mutex);
  716. goto out_unpin;
  717. }
  718. }
  719. while (remain > 0) {
  720. /* Operation in this page
  721. *
  722. * page_base = page offset within aperture
  723. * page_offset = offset within page
  724. * page_length = bytes to copy for this page
  725. */
  726. u32 page_base = node.start;
  727. unsigned page_offset = offset_in_page(offset);
  728. unsigned page_length = PAGE_SIZE - page_offset;
  729. page_length = remain < page_length ? remain : page_length;
  730. if (node.allocated) {
  731. wmb();
  732. ggtt->base.insert_page(&ggtt->base,
  733. i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT),
  734. node.start,
  735. I915_CACHE_NONE, 0);
  736. wmb();
  737. } else {
  738. page_base += offset & PAGE_MASK;
  739. }
  740. /* This is a slow read/write as it tries to read from
  741. * and write to user memory which may result into page
  742. * faults, and so we cannot perform this under struct_mutex.
  743. */
  744. if (slow_user_access(&ggtt->mappable, page_base,
  745. page_offset, user_data,
  746. page_length, false)) {
  747. ret = -EFAULT;
  748. break;
  749. }
  750. remain -= page_length;
  751. user_data += page_length;
  752. offset += page_length;
  753. }
  754. mutex_lock(&dev->struct_mutex);
  755. if (ret == 0 && (obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0) {
  756. /* The user has modified the object whilst we tried
  757. * reading from it, and we now have no idea what domain
  758. * the pages should be in. As we have just been touching
  759. * them directly, flush everything back to the GTT
  760. * domain.
  761. */
  762. ret = i915_gem_object_set_to_gtt_domain(obj, false);
  763. }
  764. out_unpin:
  765. if (node.allocated) {
  766. wmb();
  767. ggtt->base.clear_range(&ggtt->base,
  768. node.start, node.size,
  769. true);
  770. i915_gem_object_unpin_pages(obj);
  771. remove_mappable_node(&node);
  772. } else {
  773. i915_vma_unpin(vma);
  774. }
  775. out:
  776. return ret;
  777. }
  778. static int
  779. i915_gem_shmem_pread(struct drm_device *dev,
  780. struct drm_i915_gem_object *obj,
  781. struct drm_i915_gem_pread *args,
  782. struct drm_file *file)
  783. {
  784. char __user *user_data;
  785. ssize_t remain;
  786. loff_t offset;
  787. int shmem_page_offset, page_length, ret = 0;
  788. int obj_do_bit17_swizzling, page_do_bit17_swizzling;
  789. int prefaulted = 0;
  790. int needs_clflush = 0;
  791. struct sg_page_iter sg_iter;
  792. ret = i915_gem_obj_prepare_shmem_read(obj, &needs_clflush);
  793. if (ret)
  794. return ret;
  795. obj_do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
  796. user_data = u64_to_user_ptr(args->data_ptr);
  797. offset = args->offset;
  798. remain = args->size;
  799. for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents,
  800. offset >> PAGE_SHIFT) {
  801. struct page *page = sg_page_iter_page(&sg_iter);
  802. if (remain <= 0)
  803. break;
  804. /* Operation in this page
  805. *
  806. * shmem_page_offset = offset within page in shmem file
  807. * page_length = bytes to copy for this page
  808. */
  809. shmem_page_offset = offset_in_page(offset);
  810. page_length = remain;
  811. if ((shmem_page_offset + page_length) > PAGE_SIZE)
  812. page_length = PAGE_SIZE - shmem_page_offset;
  813. page_do_bit17_swizzling = obj_do_bit17_swizzling &&
  814. (page_to_phys(page) & (1 << 17)) != 0;
  815. ret = shmem_pread_fast(page, shmem_page_offset, page_length,
  816. user_data, page_do_bit17_swizzling,
  817. needs_clflush);
  818. if (ret == 0)
  819. goto next_page;
  820. mutex_unlock(&dev->struct_mutex);
  821. if (likely(!i915.prefault_disable) && !prefaulted) {
  822. ret = fault_in_pages_writeable(user_data, remain);
  823. /* Userspace is tricking us, but we've already clobbered
  824. * its pages with the prefault and promised to write the
  825. * data up to the first fault. Hence ignore any errors
  826. * and just continue. */
  827. (void)ret;
  828. prefaulted = 1;
  829. }
  830. ret = shmem_pread_slow(page, shmem_page_offset, page_length,
  831. user_data, page_do_bit17_swizzling,
  832. needs_clflush);
  833. mutex_lock(&dev->struct_mutex);
  834. if (ret)
  835. goto out;
  836. next_page:
  837. remain -= page_length;
  838. user_data += page_length;
  839. offset += page_length;
  840. }
  841. out:
  842. i915_gem_obj_finish_shmem_access(obj);
  843. return ret;
  844. }
  845. /**
  846. * Reads data from the object referenced by handle.
  847. * @dev: drm device pointer
  848. * @data: ioctl data blob
  849. * @file: drm file pointer
  850. *
  851. * On error, the contents of *data are undefined.
  852. */
  853. int
  854. i915_gem_pread_ioctl(struct drm_device *dev, void *data,
  855. struct drm_file *file)
  856. {
  857. struct drm_i915_gem_pread *args = data;
  858. struct drm_i915_gem_object *obj;
  859. int ret = 0;
  860. if (args->size == 0)
  861. return 0;
  862. if (!access_ok(VERIFY_WRITE,
  863. u64_to_user_ptr(args->data_ptr),
  864. args->size))
  865. return -EFAULT;
  866. obj = i915_gem_object_lookup(file, args->handle);
  867. if (!obj)
  868. return -ENOENT;
  869. /* Bounds check source. */
  870. if (args->offset > obj->base.size ||
  871. args->size > obj->base.size - args->offset) {
  872. ret = -EINVAL;
  873. goto err;
  874. }
  875. trace_i915_gem_object_pread(obj, args->offset, args->size);
  876. ret = __unsafe_wait_rendering(obj, to_rps_client(file), true);
  877. if (ret)
  878. goto err;
  879. ret = i915_mutex_lock_interruptible(dev);
  880. if (ret)
  881. goto err;
  882. ret = i915_gem_shmem_pread(dev, obj, args, file);
  883. /* pread for non shmem backed objects */
  884. if (ret == -EFAULT || ret == -ENODEV) {
  885. intel_runtime_pm_get(to_i915(dev));
  886. ret = i915_gem_gtt_pread(dev, obj, args->size,
  887. args->offset, args->data_ptr);
  888. intel_runtime_pm_put(to_i915(dev));
  889. }
  890. i915_gem_object_put(obj);
  891. mutex_unlock(&dev->struct_mutex);
  892. return ret;
  893. err:
  894. i915_gem_object_put_unlocked(obj);
  895. return ret;
  896. }
  897. /* This is the fast write path which cannot handle
  898. * page faults in the source data
  899. */
  900. static inline int
  901. fast_user_write(struct io_mapping *mapping,
  902. loff_t page_base, int page_offset,
  903. char __user *user_data,
  904. int length)
  905. {
  906. void __iomem *vaddr_atomic;
  907. void *vaddr;
  908. unsigned long unwritten;
  909. vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base);
  910. /* We can use the cpu mem copy function because this is X86. */
  911. vaddr = (void __force*)vaddr_atomic + page_offset;
  912. unwritten = __copy_from_user_inatomic_nocache(vaddr,
  913. user_data, length);
  914. io_mapping_unmap_atomic(vaddr_atomic);
  915. return unwritten;
  916. }
  917. /**
  918. * This is the fast pwrite path, where we copy the data directly from the
  919. * user into the GTT, uncached.
  920. * @i915: i915 device private data
  921. * @obj: i915 gem object
  922. * @args: pwrite arguments structure
  923. * @file: drm file pointer
  924. */
  925. static int
  926. i915_gem_gtt_pwrite_fast(struct drm_i915_private *i915,
  927. struct drm_i915_gem_object *obj,
  928. struct drm_i915_gem_pwrite *args,
  929. struct drm_file *file)
  930. {
  931. struct i915_ggtt *ggtt = &i915->ggtt;
  932. struct drm_device *dev = obj->base.dev;
  933. struct i915_vma *vma;
  934. struct drm_mm_node node;
  935. uint64_t remain, offset;
  936. char __user *user_data;
  937. int ret;
  938. bool hit_slow_path = false;
  939. if (i915_gem_object_is_tiled(obj))
  940. return -EFAULT;
  941. vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
  942. PIN_MAPPABLE | PIN_NONBLOCK);
  943. if (!IS_ERR(vma)) {
  944. node.start = i915_ggtt_offset(vma);
  945. node.allocated = false;
  946. ret = i915_vma_put_fence(vma);
  947. if (ret) {
  948. i915_vma_unpin(vma);
  949. vma = ERR_PTR(ret);
  950. }
  951. }
  952. if (IS_ERR(vma)) {
  953. ret = insert_mappable_node(i915, &node, PAGE_SIZE);
  954. if (ret)
  955. goto out;
  956. ret = i915_gem_object_get_pages(obj);
  957. if (ret) {
  958. remove_mappable_node(&node);
  959. goto out;
  960. }
  961. i915_gem_object_pin_pages(obj);
  962. }
  963. ret = i915_gem_object_set_to_gtt_domain(obj, true);
  964. if (ret)
  965. goto out_unpin;
  966. intel_fb_obj_invalidate(obj, ORIGIN_CPU);
  967. obj->dirty = true;
  968. user_data = u64_to_user_ptr(args->data_ptr);
  969. offset = args->offset;
  970. remain = args->size;
  971. while (remain) {
  972. /* Operation in this page
  973. *
  974. * page_base = page offset within aperture
  975. * page_offset = offset within page
  976. * page_length = bytes to copy for this page
  977. */
  978. u32 page_base = node.start;
  979. unsigned page_offset = offset_in_page(offset);
  980. unsigned page_length = PAGE_SIZE - page_offset;
  981. page_length = remain < page_length ? remain : page_length;
  982. if (node.allocated) {
  983. wmb(); /* flush the write before we modify the GGTT */
  984. ggtt->base.insert_page(&ggtt->base,
  985. i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT),
  986. node.start, I915_CACHE_NONE, 0);
  987. wmb(); /* flush modifications to the GGTT (insert_page) */
  988. } else {
  989. page_base += offset & PAGE_MASK;
  990. }
  991. /* If we get a fault while copying data, then (presumably) our
  992. * source page isn't available. Return the error and we'll
  993. * retry in the slow path.
  994. * If the object is non-shmem backed, we retry again with the
  995. * path that handles page fault.
  996. */
  997. if (fast_user_write(&ggtt->mappable, page_base,
  998. page_offset, user_data, page_length)) {
  999. hit_slow_path = true;
  1000. mutex_unlock(&dev->struct_mutex);
  1001. if (slow_user_access(&ggtt->mappable,
  1002. page_base,
  1003. page_offset, user_data,
  1004. page_length, true)) {
  1005. ret = -EFAULT;
  1006. mutex_lock(&dev->struct_mutex);
  1007. goto out_flush;
  1008. }
  1009. mutex_lock(&dev->struct_mutex);
  1010. }
  1011. remain -= page_length;
  1012. user_data += page_length;
  1013. offset += page_length;
  1014. }
  1015. out_flush:
  1016. if (hit_slow_path) {
  1017. if (ret == 0 &&
  1018. (obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0) {
  1019. /* The user has modified the object whilst we tried
  1020. * reading from it, and we now have no idea what domain
  1021. * the pages should be in. As we have just been touching
  1022. * them directly, flush everything back to the GTT
  1023. * domain.
  1024. */
  1025. ret = i915_gem_object_set_to_gtt_domain(obj, false);
  1026. }
  1027. }
  1028. intel_fb_obj_flush(obj, false, ORIGIN_CPU);
  1029. out_unpin:
  1030. if (node.allocated) {
  1031. wmb();
  1032. ggtt->base.clear_range(&ggtt->base,
  1033. node.start, node.size,
  1034. true);
  1035. i915_gem_object_unpin_pages(obj);
  1036. remove_mappable_node(&node);
  1037. } else {
  1038. i915_vma_unpin(vma);
  1039. }
  1040. out:
  1041. return ret;
  1042. }
  1043. /* Per-page copy function for the shmem pwrite fastpath.
  1044. * Flushes invalid cachelines before writing to the target if
  1045. * needs_clflush_before is set and flushes out any written cachelines after
  1046. * writing if needs_clflush is set. */
  1047. static int
  1048. shmem_pwrite_fast(struct page *page, int shmem_page_offset, int page_length,
  1049. char __user *user_data,
  1050. bool page_do_bit17_swizzling,
  1051. bool needs_clflush_before,
  1052. bool needs_clflush_after)
  1053. {
  1054. char *vaddr;
  1055. int ret;
  1056. if (unlikely(page_do_bit17_swizzling))
  1057. return -EINVAL;
  1058. vaddr = kmap_atomic(page);
  1059. if (needs_clflush_before)
  1060. drm_clflush_virt_range(vaddr + shmem_page_offset,
  1061. page_length);
  1062. ret = __copy_from_user_inatomic(vaddr + shmem_page_offset,
  1063. user_data, page_length);
  1064. if (needs_clflush_after)
  1065. drm_clflush_virt_range(vaddr + shmem_page_offset,
  1066. page_length);
  1067. kunmap_atomic(vaddr);
  1068. return ret ? -EFAULT : 0;
  1069. }
  1070. /* Only difference to the fast-path function is that this can handle bit17
  1071. * and uses non-atomic copy and kmap functions. */
  1072. static int
  1073. shmem_pwrite_slow(struct page *page, int shmem_page_offset, int page_length,
  1074. char __user *user_data,
  1075. bool page_do_bit17_swizzling,
  1076. bool needs_clflush_before,
  1077. bool needs_clflush_after)
  1078. {
  1079. char *vaddr;
  1080. int ret;
  1081. vaddr = kmap(page);
  1082. if (unlikely(needs_clflush_before || page_do_bit17_swizzling))
  1083. shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
  1084. page_length,
  1085. page_do_bit17_swizzling);
  1086. if (page_do_bit17_swizzling)
  1087. ret = __copy_from_user_swizzled(vaddr, shmem_page_offset,
  1088. user_data,
  1089. page_length);
  1090. else
  1091. ret = __copy_from_user(vaddr + shmem_page_offset,
  1092. user_data,
  1093. page_length);
  1094. if (needs_clflush_after)
  1095. shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
  1096. page_length,
  1097. page_do_bit17_swizzling);
  1098. kunmap(page);
  1099. return ret ? -EFAULT : 0;
  1100. }
  1101. static int
  1102. i915_gem_shmem_pwrite(struct drm_device *dev,
  1103. struct drm_i915_gem_object *obj,
  1104. struct drm_i915_gem_pwrite *args,
  1105. struct drm_file *file)
  1106. {
  1107. ssize_t remain;
  1108. loff_t offset;
  1109. char __user *user_data;
  1110. int shmem_page_offset, page_length, ret = 0;
  1111. int obj_do_bit17_swizzling, page_do_bit17_swizzling;
  1112. int hit_slowpath = 0;
  1113. unsigned int needs_clflush;
  1114. struct sg_page_iter sg_iter;
  1115. ret = i915_gem_obj_prepare_shmem_write(obj, &needs_clflush);
  1116. if (ret)
  1117. return ret;
  1118. obj_do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
  1119. user_data = u64_to_user_ptr(args->data_ptr);
  1120. offset = args->offset;
  1121. remain = args->size;
  1122. for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents,
  1123. offset >> PAGE_SHIFT) {
  1124. struct page *page = sg_page_iter_page(&sg_iter);
  1125. int partial_cacheline_write;
  1126. if (remain <= 0)
  1127. break;
  1128. /* Operation in this page
  1129. *
  1130. * shmem_page_offset = offset within page in shmem file
  1131. * page_length = bytes to copy for this page
  1132. */
  1133. shmem_page_offset = offset_in_page(offset);
  1134. page_length = remain;
  1135. if ((shmem_page_offset + page_length) > PAGE_SIZE)
  1136. page_length = PAGE_SIZE - shmem_page_offset;
  1137. /* If we don't overwrite a cacheline completely we need to be
  1138. * careful to have up-to-date data by first clflushing. Don't
  1139. * overcomplicate things and flush the entire patch. */
  1140. partial_cacheline_write = needs_clflush & CLFLUSH_BEFORE &&
  1141. ((shmem_page_offset | page_length)
  1142. & (boot_cpu_data.x86_clflush_size - 1));
  1143. page_do_bit17_swizzling = obj_do_bit17_swizzling &&
  1144. (page_to_phys(page) & (1 << 17)) != 0;
  1145. ret = shmem_pwrite_fast(page, shmem_page_offset, page_length,
  1146. user_data, page_do_bit17_swizzling,
  1147. partial_cacheline_write,
  1148. needs_clflush & CLFLUSH_AFTER);
  1149. if (ret == 0)
  1150. goto next_page;
  1151. hit_slowpath = 1;
  1152. mutex_unlock(&dev->struct_mutex);
  1153. ret = shmem_pwrite_slow(page, shmem_page_offset, page_length,
  1154. user_data, page_do_bit17_swizzling,
  1155. partial_cacheline_write,
  1156. needs_clflush & CLFLUSH_AFTER);
  1157. mutex_lock(&dev->struct_mutex);
  1158. if (ret)
  1159. goto out;
  1160. next_page:
  1161. remain -= page_length;
  1162. user_data += page_length;
  1163. offset += page_length;
  1164. }
  1165. out:
  1166. i915_gem_obj_finish_shmem_access(obj);
  1167. if (hit_slowpath) {
  1168. /*
  1169. * Fixup: Flush cpu caches in case we didn't flush the dirty
  1170. * cachelines in-line while writing and the object moved
  1171. * out of the cpu write domain while we've dropped the lock.
  1172. */
  1173. if (!(needs_clflush & CLFLUSH_AFTER) &&
  1174. obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
  1175. if (i915_gem_clflush_object(obj, obj->pin_display))
  1176. needs_clflush |= CLFLUSH_AFTER;
  1177. }
  1178. }
  1179. if (needs_clflush & CLFLUSH_AFTER)
  1180. i915_gem_chipset_flush(to_i915(dev));
  1181. intel_fb_obj_flush(obj, false, ORIGIN_CPU);
  1182. return ret;
  1183. }
  1184. /**
  1185. * Writes data to the object referenced by handle.
  1186. * @dev: drm device
  1187. * @data: ioctl data blob
  1188. * @file: drm file
  1189. *
  1190. * On error, the contents of the buffer that were to be modified are undefined.
  1191. */
  1192. int
  1193. i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
  1194. struct drm_file *file)
  1195. {
  1196. struct drm_i915_private *dev_priv = to_i915(dev);
  1197. struct drm_i915_gem_pwrite *args = data;
  1198. struct drm_i915_gem_object *obj;
  1199. int ret;
  1200. if (args->size == 0)
  1201. return 0;
  1202. if (!access_ok(VERIFY_READ,
  1203. u64_to_user_ptr(args->data_ptr),
  1204. args->size))
  1205. return -EFAULT;
  1206. if (likely(!i915.prefault_disable)) {
  1207. ret = fault_in_pages_readable(u64_to_user_ptr(args->data_ptr),
  1208. args->size);
  1209. if (ret)
  1210. return -EFAULT;
  1211. }
  1212. obj = i915_gem_object_lookup(file, args->handle);
  1213. if (!obj)
  1214. return -ENOENT;
  1215. /* Bounds check destination. */
  1216. if (args->offset > obj->base.size ||
  1217. args->size > obj->base.size - args->offset) {
  1218. ret = -EINVAL;
  1219. goto err;
  1220. }
  1221. trace_i915_gem_object_pwrite(obj, args->offset, args->size);
  1222. ret = __unsafe_wait_rendering(obj, to_rps_client(file), false);
  1223. if (ret)
  1224. goto err;
  1225. intel_runtime_pm_get(dev_priv);
  1226. ret = i915_mutex_lock_interruptible(dev);
  1227. if (ret)
  1228. goto err_rpm;
  1229. ret = -EFAULT;
  1230. /* We can only do the GTT pwrite on untiled buffers, as otherwise
  1231. * it would end up going through the fenced access, and we'll get
  1232. * different detiling behavior between reading and writing.
  1233. * pread/pwrite currently are reading and writing from the CPU
  1234. * perspective, requiring manual detiling by the client.
  1235. */
  1236. if (!i915_gem_object_has_struct_page(obj) ||
  1237. cpu_write_needs_clflush(obj)) {
  1238. ret = i915_gem_gtt_pwrite_fast(dev_priv, obj, args, file);
  1239. /* Note that the gtt paths might fail with non-page-backed user
  1240. * pointers (e.g. gtt mappings when moving data between
  1241. * textures). Fallback to the shmem path in that case. */
  1242. }
  1243. if (ret == -EFAULT || ret == -ENOSPC) {
  1244. if (obj->phys_handle)
  1245. ret = i915_gem_phys_pwrite(obj, args, file);
  1246. else
  1247. ret = i915_gem_shmem_pwrite(dev, obj, args, file);
  1248. }
  1249. i915_gem_object_put(obj);
  1250. mutex_unlock(&dev->struct_mutex);
  1251. intel_runtime_pm_put(dev_priv);
  1252. return ret;
  1253. err_rpm:
  1254. intel_runtime_pm_put(dev_priv);
  1255. err:
  1256. i915_gem_object_put_unlocked(obj);
  1257. return ret;
  1258. }
  1259. static inline enum fb_op_origin
  1260. write_origin(struct drm_i915_gem_object *obj, unsigned domain)
  1261. {
  1262. return (domain == I915_GEM_DOMAIN_GTT ?
  1263. obj->frontbuffer_ggtt_origin : ORIGIN_CPU);
  1264. }
  1265. /**
  1266. * Called when user space prepares to use an object with the CPU, either
  1267. * through the mmap ioctl's mapping or a GTT mapping.
  1268. * @dev: drm device
  1269. * @data: ioctl data blob
  1270. * @file: drm file
  1271. */
  1272. int
  1273. i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
  1274. struct drm_file *file)
  1275. {
  1276. struct drm_i915_gem_set_domain *args = data;
  1277. struct drm_i915_gem_object *obj;
  1278. uint32_t read_domains = args->read_domains;
  1279. uint32_t write_domain = args->write_domain;
  1280. int ret;
  1281. /* Only handle setting domains to types used by the CPU. */
  1282. if ((write_domain | read_domains) & I915_GEM_GPU_DOMAINS)
  1283. return -EINVAL;
  1284. /* Having something in the write domain implies it's in the read
  1285. * domain, and only that read domain. Enforce that in the request.
  1286. */
  1287. if (write_domain != 0 && read_domains != write_domain)
  1288. return -EINVAL;
  1289. obj = i915_gem_object_lookup(file, args->handle);
  1290. if (!obj)
  1291. return -ENOENT;
  1292. /* Try to flush the object off the GPU without holding the lock.
  1293. * We will repeat the flush holding the lock in the normal manner
  1294. * to catch cases where we are gazumped.
  1295. */
  1296. ret = __unsafe_wait_rendering(obj, to_rps_client(file), !write_domain);
  1297. if (ret)
  1298. goto err;
  1299. ret = i915_mutex_lock_interruptible(dev);
  1300. if (ret)
  1301. goto err;
  1302. if (read_domains & I915_GEM_DOMAIN_GTT)
  1303. ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
  1304. else
  1305. ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
  1306. if (write_domain != 0)
  1307. intel_fb_obj_invalidate(obj, write_origin(obj, write_domain));
  1308. i915_gem_object_put(obj);
  1309. mutex_unlock(&dev->struct_mutex);
  1310. return ret;
  1311. err:
  1312. i915_gem_object_put_unlocked(obj);
  1313. return ret;
  1314. }
  1315. /**
  1316. * Called when user space has done writes to this buffer
  1317. * @dev: drm device
  1318. * @data: ioctl data blob
  1319. * @file: drm file
  1320. */
  1321. int
  1322. i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
  1323. struct drm_file *file)
  1324. {
  1325. struct drm_i915_gem_sw_finish *args = data;
  1326. struct drm_i915_gem_object *obj;
  1327. int err = 0;
  1328. obj = i915_gem_object_lookup(file, args->handle);
  1329. if (!obj)
  1330. return -ENOENT;
  1331. /* Pinned buffers may be scanout, so flush the cache */
  1332. if (READ_ONCE(obj->pin_display)) {
  1333. err = i915_mutex_lock_interruptible(dev);
  1334. if (!err) {
  1335. i915_gem_object_flush_cpu_write_domain(obj);
  1336. mutex_unlock(&dev->struct_mutex);
  1337. }
  1338. }
  1339. i915_gem_object_put_unlocked(obj);
  1340. return err;
  1341. }
  1342. /**
  1343. * i915_gem_mmap_ioctl - Maps the contents of an object, returning the address
  1344. * it is mapped to.
  1345. * @dev: drm device
  1346. * @data: ioctl data blob
  1347. * @file: drm file
  1348. *
  1349. * While the mapping holds a reference on the contents of the object, it doesn't
  1350. * imply a ref on the object itself.
  1351. *
  1352. * IMPORTANT:
  1353. *
  1354. * DRM driver writers who look a this function as an example for how to do GEM
  1355. * mmap support, please don't implement mmap support like here. The modern way
  1356. * to implement DRM mmap support is with an mmap offset ioctl (like
  1357. * i915_gem_mmap_gtt) and then using the mmap syscall on the DRM fd directly.
  1358. * That way debug tooling like valgrind will understand what's going on, hiding
  1359. * the mmap call in a driver private ioctl will break that. The i915 driver only
  1360. * does cpu mmaps this way because we didn't know better.
  1361. */
  1362. int
  1363. i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
  1364. struct drm_file *file)
  1365. {
  1366. struct drm_i915_gem_mmap *args = data;
  1367. struct drm_i915_gem_object *obj;
  1368. unsigned long addr;
  1369. if (args->flags & ~(I915_MMAP_WC))
  1370. return -EINVAL;
  1371. if (args->flags & I915_MMAP_WC && !boot_cpu_has(X86_FEATURE_PAT))
  1372. return -ENODEV;
  1373. obj = i915_gem_object_lookup(file, args->handle);
  1374. if (!obj)
  1375. return -ENOENT;
  1376. /* prime objects have no backing filp to GEM mmap
  1377. * pages from.
  1378. */
  1379. if (!obj->base.filp) {
  1380. i915_gem_object_put_unlocked(obj);
  1381. return -EINVAL;
  1382. }
  1383. addr = vm_mmap(obj->base.filp, 0, args->size,
  1384. PROT_READ | PROT_WRITE, MAP_SHARED,
  1385. args->offset);
  1386. if (args->flags & I915_MMAP_WC) {
  1387. struct mm_struct *mm = current->mm;
  1388. struct vm_area_struct *vma;
  1389. if (down_write_killable(&mm->mmap_sem)) {
  1390. i915_gem_object_put_unlocked(obj);
  1391. return -EINTR;
  1392. }
  1393. vma = find_vma(mm, addr);
  1394. if (vma)
  1395. vma->vm_page_prot =
  1396. pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
  1397. else
  1398. addr = -ENOMEM;
  1399. up_write(&mm->mmap_sem);
  1400. /* This may race, but that's ok, it only gets set */
  1401. WRITE_ONCE(obj->frontbuffer_ggtt_origin, ORIGIN_CPU);
  1402. }
  1403. i915_gem_object_put_unlocked(obj);
  1404. if (IS_ERR((void *)addr))
  1405. return addr;
  1406. args->addr_ptr = (uint64_t) addr;
  1407. return 0;
  1408. }
  1409. static unsigned int tile_row_pages(struct drm_i915_gem_object *obj)
  1410. {
  1411. u64 size;
  1412. size = i915_gem_object_get_stride(obj);
  1413. size *= i915_gem_object_get_tiling(obj) == I915_TILING_Y ? 32 : 8;
  1414. return size >> PAGE_SHIFT;
  1415. }
  1416. /**
  1417. * i915_gem_mmap_gtt_version - report the current feature set for GTT mmaps
  1418. *
  1419. * A history of the GTT mmap interface:
  1420. *
  1421. * 0 - Everything had to fit into the GTT. Both parties of a memcpy had to
  1422. * aligned and suitable for fencing, and still fit into the available
  1423. * mappable space left by the pinned display objects. A classic problem
  1424. * we called the page-fault-of-doom where we would ping-pong between
  1425. * two objects that could not fit inside the GTT and so the memcpy
  1426. * would page one object in at the expense of the other between every
  1427. * single byte.
  1428. *
  1429. * 1 - Objects can be any size, and have any compatible fencing (X Y, or none
  1430. * as set via i915_gem_set_tiling() [DRM_I915_GEM_SET_TILING]). If the
  1431. * object is too large for the available space (or simply too large
  1432. * for the mappable aperture!), a view is created instead and faulted
  1433. * into userspace. (This view is aligned and sized appropriately for
  1434. * fenced access.)
  1435. *
  1436. * Restrictions:
  1437. *
  1438. * * snoopable objects cannot be accessed via the GTT. It can cause machine
  1439. * hangs on some architectures, corruption on others. An attempt to service
  1440. * a GTT page fault from a snoopable object will generate a SIGBUS.
  1441. *
  1442. * * the object must be able to fit into RAM (physical memory, though no
  1443. * limited to the mappable aperture).
  1444. *
  1445. *
  1446. * Caveats:
  1447. *
  1448. * * a new GTT page fault will synchronize rendering from the GPU and flush
  1449. * all data to system memory. Subsequent access will not be synchronized.
  1450. *
  1451. * * all mappings are revoked on runtime device suspend.
  1452. *
  1453. * * there are only 8, 16 or 32 fence registers to share between all users
  1454. * (older machines require fence register for display and blitter access
  1455. * as well). Contention of the fence registers will cause the previous users
  1456. * to be unmapped and any new access will generate new page faults.
  1457. *
  1458. * * running out of memory while servicing a fault may generate a SIGBUS,
  1459. * rather than the expected SIGSEGV.
  1460. */
  1461. int i915_gem_mmap_gtt_version(void)
  1462. {
  1463. return 1;
  1464. }
  1465. /**
  1466. * i915_gem_fault - fault a page into the GTT
  1467. * @area: CPU VMA in question
  1468. * @vmf: fault info
  1469. *
  1470. * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
  1471. * from userspace. The fault handler takes care of binding the object to
  1472. * the GTT (if needed), allocating and programming a fence register (again,
  1473. * only if needed based on whether the old reg is still valid or the object
  1474. * is tiled) and inserting a new PTE into the faulting process.
  1475. *
  1476. * Note that the faulting process may involve evicting existing objects
  1477. * from the GTT and/or fence registers to make room. So performance may
  1478. * suffer if the GTT working set is large or there are few fence registers
  1479. * left.
  1480. *
  1481. * The current feature set supported by i915_gem_fault() and thus GTT mmaps
  1482. * is exposed via I915_PARAM_MMAP_GTT_VERSION (see i915_gem_mmap_gtt_version).
  1483. */
  1484. int i915_gem_fault(struct vm_area_struct *area, struct vm_fault *vmf)
  1485. {
  1486. #define MIN_CHUNK_PAGES ((1 << 20) >> PAGE_SHIFT) /* 1 MiB */
  1487. struct drm_i915_gem_object *obj = to_intel_bo(area->vm_private_data);
  1488. struct drm_device *dev = obj->base.dev;
  1489. struct drm_i915_private *dev_priv = to_i915(dev);
  1490. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  1491. bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
  1492. struct i915_vma *vma;
  1493. pgoff_t page_offset;
  1494. unsigned int flags;
  1495. int ret;
  1496. /* We don't use vmf->pgoff since that has the fake offset */
  1497. page_offset = ((unsigned long)vmf->virtual_address - area->vm_start) >>
  1498. PAGE_SHIFT;
  1499. trace_i915_gem_object_fault(obj, page_offset, true, write);
  1500. /* Try to flush the object off the GPU first without holding the lock.
  1501. * Upon acquiring the lock, we will perform our sanity checks and then
  1502. * repeat the flush holding the lock in the normal manner to catch cases
  1503. * where we are gazumped.
  1504. */
  1505. ret = __unsafe_wait_rendering(obj, NULL, !write);
  1506. if (ret)
  1507. goto err;
  1508. intel_runtime_pm_get(dev_priv);
  1509. ret = i915_mutex_lock_interruptible(dev);
  1510. if (ret)
  1511. goto err_rpm;
  1512. /* Access to snoopable pages through the GTT is incoherent. */
  1513. if (obj->cache_level != I915_CACHE_NONE && !HAS_LLC(dev)) {
  1514. ret = -EFAULT;
  1515. goto err_unlock;
  1516. }
  1517. /* If the object is smaller than a couple of partial vma, it is
  1518. * not worth only creating a single partial vma - we may as well
  1519. * clear enough space for the full object.
  1520. */
  1521. flags = PIN_MAPPABLE;
  1522. if (obj->base.size > 2 * MIN_CHUNK_PAGES << PAGE_SHIFT)
  1523. flags |= PIN_NONBLOCK | PIN_NONFAULT;
  1524. /* Now pin it into the GTT as needed */
  1525. vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, flags);
  1526. if (IS_ERR(vma)) {
  1527. struct i915_ggtt_view view;
  1528. unsigned int chunk_size;
  1529. /* Use a partial view if it is bigger than available space */
  1530. chunk_size = MIN_CHUNK_PAGES;
  1531. if (i915_gem_object_is_tiled(obj))
  1532. chunk_size = roundup(chunk_size, tile_row_pages(obj));
  1533. memset(&view, 0, sizeof(view));
  1534. view.type = I915_GGTT_VIEW_PARTIAL;
  1535. view.params.partial.offset = rounddown(page_offset, chunk_size);
  1536. view.params.partial.size =
  1537. min_t(unsigned int, chunk_size,
  1538. (area->vm_end - area->vm_start) / PAGE_SIZE -
  1539. view.params.partial.offset);
  1540. /* If the partial covers the entire object, just create a
  1541. * normal VMA.
  1542. */
  1543. if (chunk_size >= obj->base.size >> PAGE_SHIFT)
  1544. view.type = I915_GGTT_VIEW_NORMAL;
  1545. /* Userspace is now writing through an untracked VMA, abandon
  1546. * all hope that the hardware is able to track future writes.
  1547. */
  1548. obj->frontbuffer_ggtt_origin = ORIGIN_CPU;
  1549. vma = i915_gem_object_ggtt_pin(obj, &view, 0, 0, PIN_MAPPABLE);
  1550. }
  1551. if (IS_ERR(vma)) {
  1552. ret = PTR_ERR(vma);
  1553. goto err_unlock;
  1554. }
  1555. ret = i915_gem_object_set_to_gtt_domain(obj, write);
  1556. if (ret)
  1557. goto err_unpin;
  1558. ret = i915_vma_get_fence(vma);
  1559. if (ret)
  1560. goto err_unpin;
  1561. /* Finally, remap it using the new GTT offset */
  1562. ret = remap_io_mapping(area,
  1563. area->vm_start + (vma->ggtt_view.params.partial.offset << PAGE_SHIFT),
  1564. (ggtt->mappable_base + vma->node.start) >> PAGE_SHIFT,
  1565. min_t(u64, vma->size, area->vm_end - area->vm_start),
  1566. &ggtt->mappable);
  1567. if (ret)
  1568. goto err_unpin;
  1569. obj->fault_mappable = true;
  1570. err_unpin:
  1571. __i915_vma_unpin(vma);
  1572. err_unlock:
  1573. mutex_unlock(&dev->struct_mutex);
  1574. err_rpm:
  1575. intel_runtime_pm_put(dev_priv);
  1576. err:
  1577. switch (ret) {
  1578. case -EIO:
  1579. /*
  1580. * We eat errors when the gpu is terminally wedged to avoid
  1581. * userspace unduly crashing (gl has no provisions for mmaps to
  1582. * fail). But any other -EIO isn't ours (e.g. swap in failure)
  1583. * and so needs to be reported.
  1584. */
  1585. if (!i915_terminally_wedged(&dev_priv->gpu_error)) {
  1586. ret = VM_FAULT_SIGBUS;
  1587. break;
  1588. }
  1589. case -EAGAIN:
  1590. /*
  1591. * EAGAIN means the gpu is hung and we'll wait for the error
  1592. * handler to reset everything when re-faulting in
  1593. * i915_mutex_lock_interruptible.
  1594. */
  1595. case 0:
  1596. case -ERESTARTSYS:
  1597. case -EINTR:
  1598. case -EBUSY:
  1599. /*
  1600. * EBUSY is ok: this just means that another thread
  1601. * already did the job.
  1602. */
  1603. ret = VM_FAULT_NOPAGE;
  1604. break;
  1605. case -ENOMEM:
  1606. ret = VM_FAULT_OOM;
  1607. break;
  1608. case -ENOSPC:
  1609. case -EFAULT:
  1610. ret = VM_FAULT_SIGBUS;
  1611. break;
  1612. default:
  1613. WARN_ONCE(ret, "unhandled error in i915_gem_fault: %i\n", ret);
  1614. ret = VM_FAULT_SIGBUS;
  1615. break;
  1616. }
  1617. return ret;
  1618. }
  1619. /**
  1620. * i915_gem_release_mmap - remove physical page mappings
  1621. * @obj: obj in question
  1622. *
  1623. * Preserve the reservation of the mmapping with the DRM core code, but
  1624. * relinquish ownership of the pages back to the system.
  1625. *
  1626. * It is vital that we remove the page mapping if we have mapped a tiled
  1627. * object through the GTT and then lose the fence register due to
  1628. * resource pressure. Similarly if the object has been moved out of the
  1629. * aperture, than pages mapped into userspace must be revoked. Removing the
  1630. * mapping will then trigger a page fault on the next user access, allowing
  1631. * fixup by i915_gem_fault().
  1632. */
  1633. void
  1634. i915_gem_release_mmap(struct drm_i915_gem_object *obj)
  1635. {
  1636. /* Serialisation between user GTT access and our code depends upon
  1637. * revoking the CPU's PTE whilst the mutex is held. The next user
  1638. * pagefault then has to wait until we release the mutex.
  1639. */
  1640. lockdep_assert_held(&obj->base.dev->struct_mutex);
  1641. if (!obj->fault_mappable)
  1642. return;
  1643. drm_vma_node_unmap(&obj->base.vma_node,
  1644. obj->base.dev->anon_inode->i_mapping);
  1645. /* Ensure that the CPU's PTE are revoked and there are not outstanding
  1646. * memory transactions from userspace before we return. The TLB
  1647. * flushing implied above by changing the PTE above *should* be
  1648. * sufficient, an extra barrier here just provides us with a bit
  1649. * of paranoid documentation about our requirement to serialise
  1650. * memory writes before touching registers / GSM.
  1651. */
  1652. wmb();
  1653. obj->fault_mappable = false;
  1654. }
  1655. void
  1656. i915_gem_release_all_mmaps(struct drm_i915_private *dev_priv)
  1657. {
  1658. struct drm_i915_gem_object *obj;
  1659. list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list)
  1660. i915_gem_release_mmap(obj);
  1661. }
  1662. /**
  1663. * i915_gem_get_ggtt_size - return required global GTT size for an object
  1664. * @dev_priv: i915 device
  1665. * @size: object size
  1666. * @tiling_mode: tiling mode
  1667. *
  1668. * Return the required global GTT size for an object, taking into account
  1669. * potential fence register mapping.
  1670. */
  1671. u64 i915_gem_get_ggtt_size(struct drm_i915_private *dev_priv,
  1672. u64 size, int tiling_mode)
  1673. {
  1674. u64 ggtt_size;
  1675. GEM_BUG_ON(size == 0);
  1676. if (INTEL_GEN(dev_priv) >= 4 ||
  1677. tiling_mode == I915_TILING_NONE)
  1678. return size;
  1679. /* Previous chips need a power-of-two fence region when tiling */
  1680. if (IS_GEN3(dev_priv))
  1681. ggtt_size = 1024*1024;
  1682. else
  1683. ggtt_size = 512*1024;
  1684. while (ggtt_size < size)
  1685. ggtt_size <<= 1;
  1686. return ggtt_size;
  1687. }
  1688. /**
  1689. * i915_gem_get_ggtt_alignment - return required global GTT alignment
  1690. * @dev_priv: i915 device
  1691. * @size: object size
  1692. * @tiling_mode: tiling mode
  1693. * @fenced: is fenced alignment required or not
  1694. *
  1695. * Return the required global GTT alignment for an object, taking into account
  1696. * potential fence register mapping.
  1697. */
  1698. u64 i915_gem_get_ggtt_alignment(struct drm_i915_private *dev_priv, u64 size,
  1699. int tiling_mode, bool fenced)
  1700. {
  1701. GEM_BUG_ON(size == 0);
  1702. /*
  1703. * Minimum alignment is 4k (GTT page size), but might be greater
  1704. * if a fence register is needed for the object.
  1705. */
  1706. if (INTEL_GEN(dev_priv) >= 4 || (!fenced && IS_G33(dev_priv)) ||
  1707. tiling_mode == I915_TILING_NONE)
  1708. return 4096;
  1709. /*
  1710. * Previous chips need to be aligned to the size of the smallest
  1711. * fence register that can contain the object.
  1712. */
  1713. return i915_gem_get_ggtt_size(dev_priv, size, tiling_mode);
  1714. }
  1715. static int i915_gem_object_create_mmap_offset(struct drm_i915_gem_object *obj)
  1716. {
  1717. struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
  1718. int err;
  1719. err = drm_gem_create_mmap_offset(&obj->base);
  1720. if (!err)
  1721. return 0;
  1722. /* We can idle the GPU locklessly to flush stale objects, but in order
  1723. * to claim that space for ourselves, we need to take the big
  1724. * struct_mutex to free the requests+objects and allocate our slot.
  1725. */
  1726. err = i915_gem_wait_for_idle(dev_priv, I915_WAIT_INTERRUPTIBLE);
  1727. if (err)
  1728. return err;
  1729. err = i915_mutex_lock_interruptible(&dev_priv->drm);
  1730. if (!err) {
  1731. i915_gem_retire_requests(dev_priv);
  1732. err = drm_gem_create_mmap_offset(&obj->base);
  1733. mutex_unlock(&dev_priv->drm.struct_mutex);
  1734. }
  1735. return err;
  1736. }
  1737. static void i915_gem_object_free_mmap_offset(struct drm_i915_gem_object *obj)
  1738. {
  1739. drm_gem_free_mmap_offset(&obj->base);
  1740. }
  1741. int
  1742. i915_gem_mmap_gtt(struct drm_file *file,
  1743. struct drm_device *dev,
  1744. uint32_t handle,
  1745. uint64_t *offset)
  1746. {
  1747. struct drm_i915_gem_object *obj;
  1748. int ret;
  1749. obj = i915_gem_object_lookup(file, handle);
  1750. if (!obj)
  1751. return -ENOENT;
  1752. ret = i915_gem_object_create_mmap_offset(obj);
  1753. if (ret == 0)
  1754. *offset = drm_vma_node_offset_addr(&obj->base.vma_node);
  1755. i915_gem_object_put_unlocked(obj);
  1756. return ret;
  1757. }
  1758. /**
  1759. * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
  1760. * @dev: DRM device
  1761. * @data: GTT mapping ioctl data
  1762. * @file: GEM object info
  1763. *
  1764. * Simply returns the fake offset to userspace so it can mmap it.
  1765. * The mmap call will end up in drm_gem_mmap(), which will set things
  1766. * up so we can get faults in the handler above.
  1767. *
  1768. * The fault handler will take care of binding the object into the GTT
  1769. * (since it may have been evicted to make room for something), allocating
  1770. * a fence register, and mapping the appropriate aperture address into
  1771. * userspace.
  1772. */
  1773. int
  1774. i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
  1775. struct drm_file *file)
  1776. {
  1777. struct drm_i915_gem_mmap_gtt *args = data;
  1778. return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset);
  1779. }
  1780. /* Immediately discard the backing storage */
  1781. static void
  1782. i915_gem_object_truncate(struct drm_i915_gem_object *obj)
  1783. {
  1784. i915_gem_object_free_mmap_offset(obj);
  1785. if (obj->base.filp == NULL)
  1786. return;
  1787. /* Our goal here is to return as much of the memory as
  1788. * is possible back to the system as we are called from OOM.
  1789. * To do this we must instruct the shmfs to drop all of its
  1790. * backing pages, *now*.
  1791. */
  1792. shmem_truncate_range(file_inode(obj->base.filp), 0, (loff_t)-1);
  1793. obj->madv = __I915_MADV_PURGED;
  1794. }
  1795. /* Try to discard unwanted pages */
  1796. static void
  1797. i915_gem_object_invalidate(struct drm_i915_gem_object *obj)
  1798. {
  1799. struct address_space *mapping;
  1800. switch (obj->madv) {
  1801. case I915_MADV_DONTNEED:
  1802. i915_gem_object_truncate(obj);
  1803. case __I915_MADV_PURGED:
  1804. return;
  1805. }
  1806. if (obj->base.filp == NULL)
  1807. return;
  1808. mapping = obj->base.filp->f_mapping,
  1809. invalidate_mapping_pages(mapping, 0, (loff_t)-1);
  1810. }
  1811. static void
  1812. i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj)
  1813. {
  1814. struct sgt_iter sgt_iter;
  1815. struct page *page;
  1816. int ret;
  1817. BUG_ON(obj->madv == __I915_MADV_PURGED);
  1818. ret = i915_gem_object_set_to_cpu_domain(obj, true);
  1819. if (WARN_ON(ret)) {
  1820. /* In the event of a disaster, abandon all caches and
  1821. * hope for the best.
  1822. */
  1823. i915_gem_clflush_object(obj, true);
  1824. obj->base.read_domains = obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  1825. }
  1826. i915_gem_gtt_finish_object(obj);
  1827. if (i915_gem_object_needs_bit17_swizzle(obj))
  1828. i915_gem_object_save_bit_17_swizzle(obj);
  1829. if (obj->madv == I915_MADV_DONTNEED)
  1830. obj->dirty = 0;
  1831. for_each_sgt_page(page, sgt_iter, obj->pages) {
  1832. if (obj->dirty)
  1833. set_page_dirty(page);
  1834. if (obj->madv == I915_MADV_WILLNEED)
  1835. mark_page_accessed(page);
  1836. put_page(page);
  1837. }
  1838. obj->dirty = 0;
  1839. sg_free_table(obj->pages);
  1840. kfree(obj->pages);
  1841. }
  1842. int
  1843. i915_gem_object_put_pages(struct drm_i915_gem_object *obj)
  1844. {
  1845. const struct drm_i915_gem_object_ops *ops = obj->ops;
  1846. if (obj->pages == NULL)
  1847. return 0;
  1848. if (obj->pages_pin_count)
  1849. return -EBUSY;
  1850. GEM_BUG_ON(obj->bind_count);
  1851. /* ->put_pages might need to allocate memory for the bit17 swizzle
  1852. * array, hence protect them from being reaped by removing them from gtt
  1853. * lists early. */
  1854. list_del(&obj->global_list);
  1855. if (obj->mapping) {
  1856. void *ptr;
  1857. ptr = ptr_mask_bits(obj->mapping);
  1858. if (is_vmalloc_addr(ptr))
  1859. vunmap(ptr);
  1860. else
  1861. kunmap(kmap_to_page(ptr));
  1862. obj->mapping = NULL;
  1863. }
  1864. ops->put_pages(obj);
  1865. obj->pages = NULL;
  1866. i915_gem_object_invalidate(obj);
  1867. return 0;
  1868. }
  1869. static int
  1870. i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj)
  1871. {
  1872. struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
  1873. int page_count, i;
  1874. struct address_space *mapping;
  1875. struct sg_table *st;
  1876. struct scatterlist *sg;
  1877. struct sgt_iter sgt_iter;
  1878. struct page *page;
  1879. unsigned long last_pfn = 0; /* suppress gcc warning */
  1880. int ret;
  1881. gfp_t gfp;
  1882. /* Assert that the object is not currently in any GPU domain. As it
  1883. * wasn't in the GTT, there shouldn't be any way it could have been in
  1884. * a GPU cache
  1885. */
  1886. BUG_ON(obj->base.read_domains & I915_GEM_GPU_DOMAINS);
  1887. BUG_ON(obj->base.write_domain & I915_GEM_GPU_DOMAINS);
  1888. st = kmalloc(sizeof(*st), GFP_KERNEL);
  1889. if (st == NULL)
  1890. return -ENOMEM;
  1891. page_count = obj->base.size / PAGE_SIZE;
  1892. if (sg_alloc_table(st, page_count, GFP_KERNEL)) {
  1893. kfree(st);
  1894. return -ENOMEM;
  1895. }
  1896. /* Get the list of pages out of our struct file. They'll be pinned
  1897. * at this point until we release them.
  1898. *
  1899. * Fail silently without starting the shrinker
  1900. */
  1901. mapping = obj->base.filp->f_mapping;
  1902. gfp = mapping_gfp_constraint(mapping, ~(__GFP_IO | __GFP_RECLAIM));
  1903. gfp |= __GFP_NORETRY | __GFP_NOWARN;
  1904. sg = st->sgl;
  1905. st->nents = 0;
  1906. for (i = 0; i < page_count; i++) {
  1907. page = shmem_read_mapping_page_gfp(mapping, i, gfp);
  1908. if (IS_ERR(page)) {
  1909. i915_gem_shrink(dev_priv,
  1910. page_count,
  1911. I915_SHRINK_BOUND |
  1912. I915_SHRINK_UNBOUND |
  1913. I915_SHRINK_PURGEABLE);
  1914. page = shmem_read_mapping_page_gfp(mapping, i, gfp);
  1915. }
  1916. if (IS_ERR(page)) {
  1917. /* We've tried hard to allocate the memory by reaping
  1918. * our own buffer, now let the real VM do its job and
  1919. * go down in flames if truly OOM.
  1920. */
  1921. i915_gem_shrink_all(dev_priv);
  1922. page = shmem_read_mapping_page(mapping, i);
  1923. if (IS_ERR(page)) {
  1924. ret = PTR_ERR(page);
  1925. goto err_sg;
  1926. }
  1927. }
  1928. #ifdef CONFIG_SWIOTLB
  1929. if (swiotlb_nr_tbl()) {
  1930. st->nents++;
  1931. sg_set_page(sg, page, PAGE_SIZE, 0);
  1932. sg = sg_next(sg);
  1933. continue;
  1934. }
  1935. #endif
  1936. if (!i || page_to_pfn(page) != last_pfn + 1) {
  1937. if (i)
  1938. sg = sg_next(sg);
  1939. st->nents++;
  1940. sg_set_page(sg, page, PAGE_SIZE, 0);
  1941. } else {
  1942. sg->length += PAGE_SIZE;
  1943. }
  1944. last_pfn = page_to_pfn(page);
  1945. /* Check that the i965g/gm workaround works. */
  1946. WARN_ON((gfp & __GFP_DMA32) && (last_pfn >= 0x00100000UL));
  1947. }
  1948. #ifdef CONFIG_SWIOTLB
  1949. if (!swiotlb_nr_tbl())
  1950. #endif
  1951. sg_mark_end(sg);
  1952. obj->pages = st;
  1953. ret = i915_gem_gtt_prepare_object(obj);
  1954. if (ret)
  1955. goto err_pages;
  1956. if (i915_gem_object_needs_bit17_swizzle(obj))
  1957. i915_gem_object_do_bit_17_swizzle(obj);
  1958. if (i915_gem_object_is_tiled(obj) &&
  1959. dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES)
  1960. i915_gem_object_pin_pages(obj);
  1961. return 0;
  1962. err_sg:
  1963. sg_mark_end(sg);
  1964. err_pages:
  1965. for_each_sgt_page(page, sgt_iter, st)
  1966. put_page(page);
  1967. sg_free_table(st);
  1968. kfree(st);
  1969. /* shmemfs first checks if there is enough memory to allocate the page
  1970. * and reports ENOSPC should there be insufficient, along with the usual
  1971. * ENOMEM for a genuine allocation failure.
  1972. *
  1973. * We use ENOSPC in our driver to mean that we have run out of aperture
  1974. * space and so want to translate the error from shmemfs back to our
  1975. * usual understanding of ENOMEM.
  1976. */
  1977. if (ret == -ENOSPC)
  1978. ret = -ENOMEM;
  1979. return ret;
  1980. }
  1981. /* Ensure that the associated pages are gathered from the backing storage
  1982. * and pinned into our object. i915_gem_object_get_pages() may be called
  1983. * multiple times before they are released by a single call to
  1984. * i915_gem_object_put_pages() - once the pages are no longer referenced
  1985. * either as a result of memory pressure (reaping pages under the shrinker)
  1986. * or as the object is itself released.
  1987. */
  1988. int
  1989. i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
  1990. {
  1991. struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
  1992. const struct drm_i915_gem_object_ops *ops = obj->ops;
  1993. int ret;
  1994. if (obj->pages)
  1995. return 0;
  1996. if (obj->madv != I915_MADV_WILLNEED) {
  1997. DRM_DEBUG("Attempting to obtain a purgeable object\n");
  1998. return -EFAULT;
  1999. }
  2000. BUG_ON(obj->pages_pin_count);
  2001. ret = ops->get_pages(obj);
  2002. if (ret)
  2003. return ret;
  2004. list_add_tail(&obj->global_list, &dev_priv->mm.unbound_list);
  2005. obj->get_page.sg = obj->pages->sgl;
  2006. obj->get_page.last = 0;
  2007. return 0;
  2008. }
  2009. /* The 'mapping' part of i915_gem_object_pin_map() below */
  2010. static void *i915_gem_object_map(const struct drm_i915_gem_object *obj,
  2011. enum i915_map_type type)
  2012. {
  2013. unsigned long n_pages = obj->base.size >> PAGE_SHIFT;
  2014. struct sg_table *sgt = obj->pages;
  2015. struct sgt_iter sgt_iter;
  2016. struct page *page;
  2017. struct page *stack_pages[32];
  2018. struct page **pages = stack_pages;
  2019. unsigned long i = 0;
  2020. pgprot_t pgprot;
  2021. void *addr;
  2022. /* A single page can always be kmapped */
  2023. if (n_pages == 1 && type == I915_MAP_WB)
  2024. return kmap(sg_page(sgt->sgl));
  2025. if (n_pages > ARRAY_SIZE(stack_pages)) {
  2026. /* Too big for stack -- allocate temporary array instead */
  2027. pages = drm_malloc_gfp(n_pages, sizeof(*pages), GFP_TEMPORARY);
  2028. if (!pages)
  2029. return NULL;
  2030. }
  2031. for_each_sgt_page(page, sgt_iter, sgt)
  2032. pages[i++] = page;
  2033. /* Check that we have the expected number of pages */
  2034. GEM_BUG_ON(i != n_pages);
  2035. switch (type) {
  2036. case I915_MAP_WB:
  2037. pgprot = PAGE_KERNEL;
  2038. break;
  2039. case I915_MAP_WC:
  2040. pgprot = pgprot_writecombine(PAGE_KERNEL_IO);
  2041. break;
  2042. }
  2043. addr = vmap(pages, n_pages, 0, pgprot);
  2044. if (pages != stack_pages)
  2045. drm_free_large(pages);
  2046. return addr;
  2047. }
  2048. /* get, pin, and map the pages of the object into kernel space */
  2049. void *i915_gem_object_pin_map(struct drm_i915_gem_object *obj,
  2050. enum i915_map_type type)
  2051. {
  2052. enum i915_map_type has_type;
  2053. bool pinned;
  2054. void *ptr;
  2055. int ret;
  2056. lockdep_assert_held(&obj->base.dev->struct_mutex);
  2057. GEM_BUG_ON(!i915_gem_object_has_struct_page(obj));
  2058. ret = i915_gem_object_get_pages(obj);
  2059. if (ret)
  2060. return ERR_PTR(ret);
  2061. i915_gem_object_pin_pages(obj);
  2062. pinned = obj->pages_pin_count > 1;
  2063. ptr = ptr_unpack_bits(obj->mapping, has_type);
  2064. if (ptr && has_type != type) {
  2065. if (pinned) {
  2066. ret = -EBUSY;
  2067. goto err;
  2068. }
  2069. if (is_vmalloc_addr(ptr))
  2070. vunmap(ptr);
  2071. else
  2072. kunmap(kmap_to_page(ptr));
  2073. ptr = obj->mapping = NULL;
  2074. }
  2075. if (!ptr) {
  2076. ptr = i915_gem_object_map(obj, type);
  2077. if (!ptr) {
  2078. ret = -ENOMEM;
  2079. goto err;
  2080. }
  2081. obj->mapping = ptr_pack_bits(ptr, type);
  2082. }
  2083. return ptr;
  2084. err:
  2085. i915_gem_object_unpin_pages(obj);
  2086. return ERR_PTR(ret);
  2087. }
  2088. static void
  2089. i915_gem_object_retire__write(struct i915_gem_active *active,
  2090. struct drm_i915_gem_request *request)
  2091. {
  2092. struct drm_i915_gem_object *obj =
  2093. container_of(active, struct drm_i915_gem_object, last_write);
  2094. intel_fb_obj_flush(obj, true, ORIGIN_CS);
  2095. }
  2096. static void
  2097. i915_gem_object_retire__read(struct i915_gem_active *active,
  2098. struct drm_i915_gem_request *request)
  2099. {
  2100. int idx = request->engine->id;
  2101. struct drm_i915_gem_object *obj =
  2102. container_of(active, struct drm_i915_gem_object, last_read[idx]);
  2103. GEM_BUG_ON(!i915_gem_object_has_active_engine(obj, idx));
  2104. i915_gem_object_clear_active(obj, idx);
  2105. if (i915_gem_object_is_active(obj))
  2106. return;
  2107. /* Bump our place on the bound list to keep it roughly in LRU order
  2108. * so that we don't steal from recently used but inactive objects
  2109. * (unless we are forced to ofc!)
  2110. */
  2111. if (obj->bind_count)
  2112. list_move_tail(&obj->global_list,
  2113. &request->i915->mm.bound_list);
  2114. i915_gem_object_put(obj);
  2115. }
  2116. static bool i915_context_is_banned(const struct i915_gem_context *ctx)
  2117. {
  2118. unsigned long elapsed;
  2119. if (ctx->hang_stats.banned)
  2120. return true;
  2121. elapsed = get_seconds() - ctx->hang_stats.guilty_ts;
  2122. if (ctx->hang_stats.ban_period_seconds &&
  2123. elapsed <= ctx->hang_stats.ban_period_seconds) {
  2124. DRM_DEBUG("context hanging too fast, banning!\n");
  2125. return true;
  2126. }
  2127. return false;
  2128. }
  2129. static void i915_set_reset_status(struct i915_gem_context *ctx,
  2130. const bool guilty)
  2131. {
  2132. struct i915_ctx_hang_stats *hs = &ctx->hang_stats;
  2133. if (guilty) {
  2134. hs->banned = i915_context_is_banned(ctx);
  2135. hs->batch_active++;
  2136. hs->guilty_ts = get_seconds();
  2137. } else {
  2138. hs->batch_pending++;
  2139. }
  2140. }
  2141. struct drm_i915_gem_request *
  2142. i915_gem_find_active_request(struct intel_engine_cs *engine)
  2143. {
  2144. struct drm_i915_gem_request *request;
  2145. /* We are called by the error capture and reset at a random
  2146. * point in time. In particular, note that neither is crucially
  2147. * ordered with an interrupt. After a hang, the GPU is dead and we
  2148. * assume that no more writes can happen (we waited long enough for
  2149. * all writes that were in transaction to be flushed) - adding an
  2150. * extra delay for a recent interrupt is pointless. Hence, we do
  2151. * not need an engine->irq_seqno_barrier() before the seqno reads.
  2152. */
  2153. list_for_each_entry(request, &engine->request_list, link) {
  2154. if (i915_gem_request_completed(request))
  2155. continue;
  2156. if (!i915_sw_fence_done(&request->submit))
  2157. break;
  2158. return request;
  2159. }
  2160. return NULL;
  2161. }
  2162. static void reset_request(struct drm_i915_gem_request *request)
  2163. {
  2164. void *vaddr = request->ring->vaddr;
  2165. u32 head;
  2166. /* As this request likely depends on state from the lost
  2167. * context, clear out all the user operations leaving the
  2168. * breadcrumb at the end (so we get the fence notifications).
  2169. */
  2170. head = request->head;
  2171. if (request->postfix < head) {
  2172. memset(vaddr + head, 0, request->ring->size - head);
  2173. head = 0;
  2174. }
  2175. memset(vaddr + head, 0, request->postfix - head);
  2176. }
  2177. static void i915_gem_reset_engine(struct intel_engine_cs *engine)
  2178. {
  2179. struct drm_i915_gem_request *request;
  2180. struct i915_gem_context *incomplete_ctx;
  2181. bool ring_hung;
  2182. /* Ensure irq handler finishes, and not run again. */
  2183. tasklet_kill(&engine->irq_tasklet);
  2184. if (engine->irq_seqno_barrier)
  2185. engine->irq_seqno_barrier(engine);
  2186. request = i915_gem_find_active_request(engine);
  2187. if (!request)
  2188. return;
  2189. ring_hung = engine->hangcheck.score >= HANGCHECK_SCORE_RING_HUNG;
  2190. i915_set_reset_status(request->ctx, ring_hung);
  2191. if (!ring_hung)
  2192. return;
  2193. DRM_DEBUG_DRIVER("resetting %s to restart from tail of request 0x%x\n",
  2194. engine->name, request->fence.seqno);
  2195. /* Setup the CS to resume from the breadcrumb of the hung request */
  2196. engine->reset_hw(engine, request);
  2197. /* Users of the default context do not rely on logical state
  2198. * preserved between batches. They have to emit full state on
  2199. * every batch and so it is safe to execute queued requests following
  2200. * the hang.
  2201. *
  2202. * Other contexts preserve state, now corrupt. We want to skip all
  2203. * queued requests that reference the corrupt context.
  2204. */
  2205. incomplete_ctx = request->ctx;
  2206. if (i915_gem_context_is_default(incomplete_ctx))
  2207. return;
  2208. list_for_each_entry_continue(request, &engine->request_list, link)
  2209. if (request->ctx == incomplete_ctx)
  2210. reset_request(request);
  2211. }
  2212. void i915_gem_reset(struct drm_i915_private *dev_priv)
  2213. {
  2214. struct intel_engine_cs *engine;
  2215. i915_gem_retire_requests(dev_priv);
  2216. for_each_engine(engine, dev_priv)
  2217. i915_gem_reset_engine(engine);
  2218. i915_gem_restore_fences(&dev_priv->drm);
  2219. if (dev_priv->gt.awake) {
  2220. intel_sanitize_gt_powersave(dev_priv);
  2221. intel_enable_gt_powersave(dev_priv);
  2222. if (INTEL_GEN(dev_priv) >= 6)
  2223. gen6_rps_busy(dev_priv);
  2224. }
  2225. }
  2226. static void nop_submit_request(struct drm_i915_gem_request *request)
  2227. {
  2228. }
  2229. static void i915_gem_cleanup_engine(struct intel_engine_cs *engine)
  2230. {
  2231. engine->submit_request = nop_submit_request;
  2232. /* Mark all pending requests as complete so that any concurrent
  2233. * (lockless) lookup doesn't try and wait upon the request as we
  2234. * reset it.
  2235. */
  2236. intel_engine_init_seqno(engine, engine->last_submitted_seqno);
  2237. /*
  2238. * Clear the execlists queue up before freeing the requests, as those
  2239. * are the ones that keep the context and ringbuffer backing objects
  2240. * pinned in place.
  2241. */
  2242. if (i915.enable_execlists) {
  2243. spin_lock(&engine->execlist_lock);
  2244. INIT_LIST_HEAD(&engine->execlist_queue);
  2245. i915_gem_request_put(engine->execlist_port[0].request);
  2246. i915_gem_request_put(engine->execlist_port[1].request);
  2247. memset(engine->execlist_port, 0, sizeof(engine->execlist_port));
  2248. spin_unlock(&engine->execlist_lock);
  2249. }
  2250. engine->i915->gt.active_engines &= ~intel_engine_flag(engine);
  2251. }
  2252. void i915_gem_set_wedged(struct drm_i915_private *dev_priv)
  2253. {
  2254. struct intel_engine_cs *engine;
  2255. lockdep_assert_held(&dev_priv->drm.struct_mutex);
  2256. set_bit(I915_WEDGED, &dev_priv->gpu_error.flags);
  2257. i915_gem_context_lost(dev_priv);
  2258. for_each_engine(engine, dev_priv)
  2259. i915_gem_cleanup_engine(engine);
  2260. mod_delayed_work(dev_priv->wq, &dev_priv->gt.idle_work, 0);
  2261. i915_gem_retire_requests(dev_priv);
  2262. }
  2263. static void
  2264. i915_gem_retire_work_handler(struct work_struct *work)
  2265. {
  2266. struct drm_i915_private *dev_priv =
  2267. container_of(work, typeof(*dev_priv), gt.retire_work.work);
  2268. struct drm_device *dev = &dev_priv->drm;
  2269. /* Come back later if the device is busy... */
  2270. if (mutex_trylock(&dev->struct_mutex)) {
  2271. i915_gem_retire_requests(dev_priv);
  2272. mutex_unlock(&dev->struct_mutex);
  2273. }
  2274. /* Keep the retire handler running until we are finally idle.
  2275. * We do not need to do this test under locking as in the worst-case
  2276. * we queue the retire worker once too often.
  2277. */
  2278. if (READ_ONCE(dev_priv->gt.awake)) {
  2279. i915_queue_hangcheck(dev_priv);
  2280. queue_delayed_work(dev_priv->wq,
  2281. &dev_priv->gt.retire_work,
  2282. round_jiffies_up_relative(HZ));
  2283. }
  2284. }
  2285. static void
  2286. i915_gem_idle_work_handler(struct work_struct *work)
  2287. {
  2288. struct drm_i915_private *dev_priv =
  2289. container_of(work, typeof(*dev_priv), gt.idle_work.work);
  2290. struct drm_device *dev = &dev_priv->drm;
  2291. struct intel_engine_cs *engine;
  2292. bool rearm_hangcheck;
  2293. if (!READ_ONCE(dev_priv->gt.awake))
  2294. return;
  2295. if (READ_ONCE(dev_priv->gt.active_engines))
  2296. return;
  2297. rearm_hangcheck =
  2298. cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work);
  2299. if (!mutex_trylock(&dev->struct_mutex)) {
  2300. /* Currently busy, come back later */
  2301. mod_delayed_work(dev_priv->wq,
  2302. &dev_priv->gt.idle_work,
  2303. msecs_to_jiffies(50));
  2304. goto out_rearm;
  2305. }
  2306. if (dev_priv->gt.active_engines)
  2307. goto out_unlock;
  2308. for_each_engine(engine, dev_priv)
  2309. i915_gem_batch_pool_fini(&engine->batch_pool);
  2310. GEM_BUG_ON(!dev_priv->gt.awake);
  2311. dev_priv->gt.awake = false;
  2312. rearm_hangcheck = false;
  2313. if (INTEL_GEN(dev_priv) >= 6)
  2314. gen6_rps_idle(dev_priv);
  2315. intel_runtime_pm_put(dev_priv);
  2316. out_unlock:
  2317. mutex_unlock(&dev->struct_mutex);
  2318. out_rearm:
  2319. if (rearm_hangcheck) {
  2320. GEM_BUG_ON(!dev_priv->gt.awake);
  2321. i915_queue_hangcheck(dev_priv);
  2322. }
  2323. }
  2324. void i915_gem_close_object(struct drm_gem_object *gem, struct drm_file *file)
  2325. {
  2326. struct drm_i915_gem_object *obj = to_intel_bo(gem);
  2327. struct drm_i915_file_private *fpriv = file->driver_priv;
  2328. struct i915_vma *vma, *vn;
  2329. mutex_lock(&obj->base.dev->struct_mutex);
  2330. list_for_each_entry_safe(vma, vn, &obj->vma_list, obj_link)
  2331. if (vma->vm->file == fpriv)
  2332. i915_vma_close(vma);
  2333. mutex_unlock(&obj->base.dev->struct_mutex);
  2334. }
  2335. /**
  2336. * i915_gem_wait_ioctl - implements DRM_IOCTL_I915_GEM_WAIT
  2337. * @dev: drm device pointer
  2338. * @data: ioctl data blob
  2339. * @file: drm file pointer
  2340. *
  2341. * Returns 0 if successful, else an error is returned with the remaining time in
  2342. * the timeout parameter.
  2343. * -ETIME: object is still busy after timeout
  2344. * -ERESTARTSYS: signal interrupted the wait
  2345. * -ENONENT: object doesn't exist
  2346. * Also possible, but rare:
  2347. * -EAGAIN: GPU wedged
  2348. * -ENOMEM: damn
  2349. * -ENODEV: Internal IRQ fail
  2350. * -E?: The add request failed
  2351. *
  2352. * The wait ioctl with a timeout of 0 reimplements the busy ioctl. With any
  2353. * non-zero timeout parameter the wait ioctl will wait for the given number of
  2354. * nanoseconds on an object becoming unbusy. Since the wait itself does so
  2355. * without holding struct_mutex the object may become re-busied before this
  2356. * function completes. A similar but shorter * race condition exists in the busy
  2357. * ioctl
  2358. */
  2359. int
  2360. i915_gem_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
  2361. {
  2362. struct drm_i915_gem_wait *args = data;
  2363. struct intel_rps_client *rps = to_rps_client(file);
  2364. struct drm_i915_gem_object *obj;
  2365. unsigned long active;
  2366. int idx, ret = 0;
  2367. if (args->flags != 0)
  2368. return -EINVAL;
  2369. obj = i915_gem_object_lookup(file, args->bo_handle);
  2370. if (!obj)
  2371. return -ENOENT;
  2372. active = __I915_BO_ACTIVE(obj);
  2373. for_each_active(active, idx) {
  2374. s64 *timeout = args->timeout_ns >= 0 ? &args->timeout_ns : NULL;
  2375. ret = i915_gem_active_wait_unlocked(&obj->last_read[idx],
  2376. I915_WAIT_INTERRUPTIBLE,
  2377. timeout, rps);
  2378. if (ret)
  2379. break;
  2380. }
  2381. i915_gem_object_put_unlocked(obj);
  2382. return ret;
  2383. }
  2384. static void __i915_vma_iounmap(struct i915_vma *vma)
  2385. {
  2386. GEM_BUG_ON(i915_vma_is_pinned(vma));
  2387. if (vma->iomap == NULL)
  2388. return;
  2389. io_mapping_unmap(vma->iomap);
  2390. vma->iomap = NULL;
  2391. }
  2392. int i915_vma_unbind(struct i915_vma *vma)
  2393. {
  2394. struct drm_i915_gem_object *obj = vma->obj;
  2395. unsigned long active;
  2396. int ret;
  2397. /* First wait upon any activity as retiring the request may
  2398. * have side-effects such as unpinning or even unbinding this vma.
  2399. */
  2400. active = i915_vma_get_active(vma);
  2401. if (active) {
  2402. int idx;
  2403. /* When a closed VMA is retired, it is unbound - eek.
  2404. * In order to prevent it from being recursively closed,
  2405. * take a pin on the vma so that the second unbind is
  2406. * aborted.
  2407. */
  2408. __i915_vma_pin(vma);
  2409. for_each_active(active, idx) {
  2410. ret = i915_gem_active_retire(&vma->last_read[idx],
  2411. &vma->vm->dev->struct_mutex);
  2412. if (ret)
  2413. break;
  2414. }
  2415. __i915_vma_unpin(vma);
  2416. if (ret)
  2417. return ret;
  2418. GEM_BUG_ON(i915_vma_is_active(vma));
  2419. }
  2420. if (i915_vma_is_pinned(vma))
  2421. return -EBUSY;
  2422. if (!drm_mm_node_allocated(&vma->node))
  2423. goto destroy;
  2424. GEM_BUG_ON(obj->bind_count == 0);
  2425. GEM_BUG_ON(!obj->pages);
  2426. if (i915_vma_is_map_and_fenceable(vma)) {
  2427. /* release the fence reg _after_ flushing */
  2428. ret = i915_vma_put_fence(vma);
  2429. if (ret)
  2430. return ret;
  2431. /* Force a pagefault for domain tracking on next user access */
  2432. i915_gem_release_mmap(obj);
  2433. __i915_vma_iounmap(vma);
  2434. vma->flags &= ~I915_VMA_CAN_FENCE;
  2435. }
  2436. if (likely(!vma->vm->closed)) {
  2437. trace_i915_vma_unbind(vma);
  2438. vma->vm->unbind_vma(vma);
  2439. }
  2440. vma->flags &= ~(I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND);
  2441. drm_mm_remove_node(&vma->node);
  2442. list_move_tail(&vma->vm_link, &vma->vm->unbound_list);
  2443. if (vma->pages != obj->pages) {
  2444. GEM_BUG_ON(!vma->pages);
  2445. sg_free_table(vma->pages);
  2446. kfree(vma->pages);
  2447. }
  2448. vma->pages = NULL;
  2449. /* Since the unbound list is global, only move to that list if
  2450. * no more VMAs exist. */
  2451. if (--obj->bind_count == 0)
  2452. list_move_tail(&obj->global_list,
  2453. &to_i915(obj->base.dev)->mm.unbound_list);
  2454. /* And finally now the object is completely decoupled from this vma,
  2455. * we can drop its hold on the backing storage and allow it to be
  2456. * reaped by the shrinker.
  2457. */
  2458. i915_gem_object_unpin_pages(obj);
  2459. destroy:
  2460. if (unlikely(i915_vma_is_closed(vma)))
  2461. i915_vma_destroy(vma);
  2462. return 0;
  2463. }
  2464. int i915_gem_wait_for_idle(struct drm_i915_private *dev_priv,
  2465. unsigned int flags)
  2466. {
  2467. struct intel_engine_cs *engine;
  2468. int ret;
  2469. for_each_engine(engine, dev_priv) {
  2470. if (engine->last_context == NULL)
  2471. continue;
  2472. ret = intel_engine_idle(engine, flags);
  2473. if (ret)
  2474. return ret;
  2475. }
  2476. return 0;
  2477. }
  2478. static bool i915_gem_valid_gtt_space(struct i915_vma *vma,
  2479. unsigned long cache_level)
  2480. {
  2481. struct drm_mm_node *gtt_space = &vma->node;
  2482. struct drm_mm_node *other;
  2483. /*
  2484. * On some machines we have to be careful when putting differing types
  2485. * of snoopable memory together to avoid the prefetcher crossing memory
  2486. * domains and dying. During vm initialisation, we decide whether or not
  2487. * these constraints apply and set the drm_mm.color_adjust
  2488. * appropriately.
  2489. */
  2490. if (vma->vm->mm.color_adjust == NULL)
  2491. return true;
  2492. if (!drm_mm_node_allocated(gtt_space))
  2493. return true;
  2494. if (list_empty(&gtt_space->node_list))
  2495. return true;
  2496. other = list_entry(gtt_space->node_list.prev, struct drm_mm_node, node_list);
  2497. if (other->allocated && !other->hole_follows && other->color != cache_level)
  2498. return false;
  2499. other = list_entry(gtt_space->node_list.next, struct drm_mm_node, node_list);
  2500. if (other->allocated && !gtt_space->hole_follows && other->color != cache_level)
  2501. return false;
  2502. return true;
  2503. }
  2504. /**
  2505. * i915_vma_insert - finds a slot for the vma in its address space
  2506. * @vma: the vma
  2507. * @size: requested size in bytes (can be larger than the VMA)
  2508. * @alignment: required alignment
  2509. * @flags: mask of PIN_* flags to use
  2510. *
  2511. * First we try to allocate some free space that meets the requirements for
  2512. * the VMA. Failiing that, if the flags permit, it will evict an old VMA,
  2513. * preferrably the oldest idle entry to make room for the new VMA.
  2514. *
  2515. * Returns:
  2516. * 0 on success, negative error code otherwise.
  2517. */
  2518. static int
  2519. i915_vma_insert(struct i915_vma *vma, u64 size, u64 alignment, u64 flags)
  2520. {
  2521. struct drm_i915_private *dev_priv = to_i915(vma->vm->dev);
  2522. struct drm_i915_gem_object *obj = vma->obj;
  2523. u64 start, end;
  2524. int ret;
  2525. GEM_BUG_ON(vma->flags & (I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND));
  2526. GEM_BUG_ON(drm_mm_node_allocated(&vma->node));
  2527. size = max(size, vma->size);
  2528. if (flags & PIN_MAPPABLE)
  2529. size = i915_gem_get_ggtt_size(dev_priv, size,
  2530. i915_gem_object_get_tiling(obj));
  2531. alignment = max(max(alignment, vma->display_alignment),
  2532. i915_gem_get_ggtt_alignment(dev_priv, size,
  2533. i915_gem_object_get_tiling(obj),
  2534. flags & PIN_MAPPABLE));
  2535. start = flags & PIN_OFFSET_BIAS ? flags & PIN_OFFSET_MASK : 0;
  2536. end = vma->vm->total;
  2537. if (flags & PIN_MAPPABLE)
  2538. end = min_t(u64, end, dev_priv->ggtt.mappable_end);
  2539. if (flags & PIN_ZONE_4G)
  2540. end = min_t(u64, end, (1ULL << 32) - PAGE_SIZE);
  2541. /* If binding the object/GGTT view requires more space than the entire
  2542. * aperture has, reject it early before evicting everything in a vain
  2543. * attempt to find space.
  2544. */
  2545. if (size > end) {
  2546. DRM_DEBUG("Attempting to bind an object larger than the aperture: request=%llu [object=%zd] > %s aperture=%llu\n",
  2547. size, obj->base.size,
  2548. flags & PIN_MAPPABLE ? "mappable" : "total",
  2549. end);
  2550. return -E2BIG;
  2551. }
  2552. ret = i915_gem_object_get_pages(obj);
  2553. if (ret)
  2554. return ret;
  2555. i915_gem_object_pin_pages(obj);
  2556. if (flags & PIN_OFFSET_FIXED) {
  2557. u64 offset = flags & PIN_OFFSET_MASK;
  2558. if (offset & (alignment - 1) || offset > end - size) {
  2559. ret = -EINVAL;
  2560. goto err_unpin;
  2561. }
  2562. vma->node.start = offset;
  2563. vma->node.size = size;
  2564. vma->node.color = obj->cache_level;
  2565. ret = drm_mm_reserve_node(&vma->vm->mm, &vma->node);
  2566. if (ret) {
  2567. ret = i915_gem_evict_for_vma(vma);
  2568. if (ret == 0)
  2569. ret = drm_mm_reserve_node(&vma->vm->mm, &vma->node);
  2570. if (ret)
  2571. goto err_unpin;
  2572. }
  2573. } else {
  2574. u32 search_flag, alloc_flag;
  2575. if (flags & PIN_HIGH) {
  2576. search_flag = DRM_MM_SEARCH_BELOW;
  2577. alloc_flag = DRM_MM_CREATE_TOP;
  2578. } else {
  2579. search_flag = DRM_MM_SEARCH_DEFAULT;
  2580. alloc_flag = DRM_MM_CREATE_DEFAULT;
  2581. }
  2582. /* We only allocate in PAGE_SIZE/GTT_PAGE_SIZE (4096) chunks,
  2583. * so we know that we always have a minimum alignment of 4096.
  2584. * The drm_mm range manager is optimised to return results
  2585. * with zero alignment, so where possible use the optimal
  2586. * path.
  2587. */
  2588. if (alignment <= 4096)
  2589. alignment = 0;
  2590. search_free:
  2591. ret = drm_mm_insert_node_in_range_generic(&vma->vm->mm,
  2592. &vma->node,
  2593. size, alignment,
  2594. obj->cache_level,
  2595. start, end,
  2596. search_flag,
  2597. alloc_flag);
  2598. if (ret) {
  2599. ret = i915_gem_evict_something(vma->vm, size, alignment,
  2600. obj->cache_level,
  2601. start, end,
  2602. flags);
  2603. if (ret == 0)
  2604. goto search_free;
  2605. goto err_unpin;
  2606. }
  2607. }
  2608. GEM_BUG_ON(!i915_gem_valid_gtt_space(vma, obj->cache_level));
  2609. list_move_tail(&obj->global_list, &dev_priv->mm.bound_list);
  2610. list_move_tail(&vma->vm_link, &vma->vm->inactive_list);
  2611. obj->bind_count++;
  2612. return 0;
  2613. err_unpin:
  2614. i915_gem_object_unpin_pages(obj);
  2615. return ret;
  2616. }
  2617. bool
  2618. i915_gem_clflush_object(struct drm_i915_gem_object *obj,
  2619. bool force)
  2620. {
  2621. /* If we don't have a page list set up, then we're not pinned
  2622. * to GPU, and we can ignore the cache flush because it'll happen
  2623. * again at bind time.
  2624. */
  2625. if (obj->pages == NULL)
  2626. return false;
  2627. /*
  2628. * Stolen memory is always coherent with the GPU as it is explicitly
  2629. * marked as wc by the system, or the system is cache-coherent.
  2630. */
  2631. if (obj->stolen || obj->phys_handle)
  2632. return false;
  2633. /* If the GPU is snooping the contents of the CPU cache,
  2634. * we do not need to manually clear the CPU cache lines. However,
  2635. * the caches are only snooped when the render cache is
  2636. * flushed/invalidated. As we always have to emit invalidations
  2637. * and flushes when moving into and out of the RENDER domain, correct
  2638. * snooping behaviour occurs naturally as the result of our domain
  2639. * tracking.
  2640. */
  2641. if (!force && cpu_cache_is_coherent(obj->base.dev, obj->cache_level)) {
  2642. obj->cache_dirty = true;
  2643. return false;
  2644. }
  2645. trace_i915_gem_object_clflush(obj);
  2646. drm_clflush_sg(obj->pages);
  2647. obj->cache_dirty = false;
  2648. return true;
  2649. }
  2650. /** Flushes the GTT write domain for the object if it's dirty. */
  2651. static void
  2652. i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj)
  2653. {
  2654. struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
  2655. if (obj->base.write_domain != I915_GEM_DOMAIN_GTT)
  2656. return;
  2657. /* No actual flushing is required for the GTT write domain. Writes
  2658. * to it "immediately" go to main memory as far as we know, so there's
  2659. * no chipset flush. It also doesn't land in render cache.
  2660. *
  2661. * However, we do have to enforce the order so that all writes through
  2662. * the GTT land before any writes to the device, such as updates to
  2663. * the GATT itself.
  2664. *
  2665. * We also have to wait a bit for the writes to land from the GTT.
  2666. * An uncached read (i.e. mmio) seems to be ideal for the round-trip
  2667. * timing. This issue has only been observed when switching quickly
  2668. * between GTT writes and CPU reads from inside the kernel on recent hw,
  2669. * and it appears to only affect discrete GTT blocks (i.e. on LLC
  2670. * system agents we cannot reproduce this behaviour).
  2671. */
  2672. wmb();
  2673. if (INTEL_GEN(dev_priv) >= 6 && !HAS_LLC(dev_priv))
  2674. POSTING_READ(RING_ACTHD(dev_priv->engine[RCS].mmio_base));
  2675. intel_fb_obj_flush(obj, false, write_origin(obj, I915_GEM_DOMAIN_GTT));
  2676. obj->base.write_domain = 0;
  2677. trace_i915_gem_object_change_domain(obj,
  2678. obj->base.read_domains,
  2679. I915_GEM_DOMAIN_GTT);
  2680. }
  2681. /** Flushes the CPU write domain for the object if it's dirty. */
  2682. static void
  2683. i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj)
  2684. {
  2685. if (obj->base.write_domain != I915_GEM_DOMAIN_CPU)
  2686. return;
  2687. if (i915_gem_clflush_object(obj, obj->pin_display))
  2688. i915_gem_chipset_flush(to_i915(obj->base.dev));
  2689. intel_fb_obj_flush(obj, false, ORIGIN_CPU);
  2690. obj->base.write_domain = 0;
  2691. trace_i915_gem_object_change_domain(obj,
  2692. obj->base.read_domains,
  2693. I915_GEM_DOMAIN_CPU);
  2694. }
  2695. static void i915_gem_object_bump_inactive_ggtt(struct drm_i915_gem_object *obj)
  2696. {
  2697. struct i915_vma *vma;
  2698. list_for_each_entry(vma, &obj->vma_list, obj_link) {
  2699. if (!i915_vma_is_ggtt(vma))
  2700. continue;
  2701. if (i915_vma_is_active(vma))
  2702. continue;
  2703. if (!drm_mm_node_allocated(&vma->node))
  2704. continue;
  2705. list_move_tail(&vma->vm_link, &vma->vm->inactive_list);
  2706. }
  2707. }
  2708. /**
  2709. * Moves a single object to the GTT read, and possibly write domain.
  2710. * @obj: object to act on
  2711. * @write: ask for write access or read only
  2712. *
  2713. * This function returns when the move is complete, including waiting on
  2714. * flushes to occur.
  2715. */
  2716. int
  2717. i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write)
  2718. {
  2719. uint32_t old_write_domain, old_read_domains;
  2720. int ret;
  2721. ret = i915_gem_object_wait_rendering(obj, !write);
  2722. if (ret)
  2723. return ret;
  2724. if (obj->base.write_domain == I915_GEM_DOMAIN_GTT)
  2725. return 0;
  2726. /* Flush and acquire obj->pages so that we are coherent through
  2727. * direct access in memory with previous cached writes through
  2728. * shmemfs and that our cache domain tracking remains valid.
  2729. * For example, if the obj->filp was moved to swap without us
  2730. * being notified and releasing the pages, we would mistakenly
  2731. * continue to assume that the obj remained out of the CPU cached
  2732. * domain.
  2733. */
  2734. ret = i915_gem_object_get_pages(obj);
  2735. if (ret)
  2736. return ret;
  2737. i915_gem_object_flush_cpu_write_domain(obj);
  2738. /* Serialise direct access to this object with the barriers for
  2739. * coherent writes from the GPU, by effectively invalidating the
  2740. * GTT domain upon first access.
  2741. */
  2742. if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0)
  2743. mb();
  2744. old_write_domain = obj->base.write_domain;
  2745. old_read_domains = obj->base.read_domains;
  2746. /* It should now be out of any other write domains, and we can update
  2747. * the domain values for our changes.
  2748. */
  2749. BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
  2750. obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
  2751. if (write) {
  2752. obj->base.read_domains = I915_GEM_DOMAIN_GTT;
  2753. obj->base.write_domain = I915_GEM_DOMAIN_GTT;
  2754. obj->dirty = 1;
  2755. }
  2756. trace_i915_gem_object_change_domain(obj,
  2757. old_read_domains,
  2758. old_write_domain);
  2759. /* And bump the LRU for this access */
  2760. i915_gem_object_bump_inactive_ggtt(obj);
  2761. return 0;
  2762. }
  2763. /**
  2764. * Changes the cache-level of an object across all VMA.
  2765. * @obj: object to act on
  2766. * @cache_level: new cache level to set for the object
  2767. *
  2768. * After this function returns, the object will be in the new cache-level
  2769. * across all GTT and the contents of the backing storage will be coherent,
  2770. * with respect to the new cache-level. In order to keep the backing storage
  2771. * coherent for all users, we only allow a single cache level to be set
  2772. * globally on the object and prevent it from being changed whilst the
  2773. * hardware is reading from the object. That is if the object is currently
  2774. * on the scanout it will be set to uncached (or equivalent display
  2775. * cache coherency) and all non-MOCS GPU access will also be uncached so
  2776. * that all direct access to the scanout remains coherent.
  2777. */
  2778. int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj,
  2779. enum i915_cache_level cache_level)
  2780. {
  2781. struct i915_vma *vma;
  2782. int ret = 0;
  2783. if (obj->cache_level == cache_level)
  2784. goto out;
  2785. /* Inspect the list of currently bound VMA and unbind any that would
  2786. * be invalid given the new cache-level. This is principally to
  2787. * catch the issue of the CS prefetch crossing page boundaries and
  2788. * reading an invalid PTE on older architectures.
  2789. */
  2790. restart:
  2791. list_for_each_entry(vma, &obj->vma_list, obj_link) {
  2792. if (!drm_mm_node_allocated(&vma->node))
  2793. continue;
  2794. if (i915_vma_is_pinned(vma)) {
  2795. DRM_DEBUG("can not change the cache level of pinned objects\n");
  2796. return -EBUSY;
  2797. }
  2798. if (i915_gem_valid_gtt_space(vma, cache_level))
  2799. continue;
  2800. ret = i915_vma_unbind(vma);
  2801. if (ret)
  2802. return ret;
  2803. /* As unbinding may affect other elements in the
  2804. * obj->vma_list (due to side-effects from retiring
  2805. * an active vma), play safe and restart the iterator.
  2806. */
  2807. goto restart;
  2808. }
  2809. /* We can reuse the existing drm_mm nodes but need to change the
  2810. * cache-level on the PTE. We could simply unbind them all and
  2811. * rebind with the correct cache-level on next use. However since
  2812. * we already have a valid slot, dma mapping, pages etc, we may as
  2813. * rewrite the PTE in the belief that doing so tramples upon less
  2814. * state and so involves less work.
  2815. */
  2816. if (obj->bind_count) {
  2817. /* Before we change the PTE, the GPU must not be accessing it.
  2818. * If we wait upon the object, we know that all the bound
  2819. * VMA are no longer active.
  2820. */
  2821. ret = i915_gem_object_wait_rendering(obj, false);
  2822. if (ret)
  2823. return ret;
  2824. if (!HAS_LLC(obj->base.dev) && cache_level != I915_CACHE_NONE) {
  2825. /* Access to snoopable pages through the GTT is
  2826. * incoherent and on some machines causes a hard
  2827. * lockup. Relinquish the CPU mmaping to force
  2828. * userspace to refault in the pages and we can
  2829. * then double check if the GTT mapping is still
  2830. * valid for that pointer access.
  2831. */
  2832. i915_gem_release_mmap(obj);
  2833. /* As we no longer need a fence for GTT access,
  2834. * we can relinquish it now (and so prevent having
  2835. * to steal a fence from someone else on the next
  2836. * fence request). Note GPU activity would have
  2837. * dropped the fence as all snoopable access is
  2838. * supposed to be linear.
  2839. */
  2840. list_for_each_entry(vma, &obj->vma_list, obj_link) {
  2841. ret = i915_vma_put_fence(vma);
  2842. if (ret)
  2843. return ret;
  2844. }
  2845. } else {
  2846. /* We either have incoherent backing store and
  2847. * so no GTT access or the architecture is fully
  2848. * coherent. In such cases, existing GTT mmaps
  2849. * ignore the cache bit in the PTE and we can
  2850. * rewrite it without confusing the GPU or having
  2851. * to force userspace to fault back in its mmaps.
  2852. */
  2853. }
  2854. list_for_each_entry(vma, &obj->vma_list, obj_link) {
  2855. if (!drm_mm_node_allocated(&vma->node))
  2856. continue;
  2857. ret = i915_vma_bind(vma, cache_level, PIN_UPDATE);
  2858. if (ret)
  2859. return ret;
  2860. }
  2861. }
  2862. list_for_each_entry(vma, &obj->vma_list, obj_link)
  2863. vma->node.color = cache_level;
  2864. obj->cache_level = cache_level;
  2865. out:
  2866. /* Flush the dirty CPU caches to the backing storage so that the
  2867. * object is now coherent at its new cache level (with respect
  2868. * to the access domain).
  2869. */
  2870. if (obj->cache_dirty && cpu_write_needs_clflush(obj)) {
  2871. if (i915_gem_clflush_object(obj, true))
  2872. i915_gem_chipset_flush(to_i915(obj->base.dev));
  2873. }
  2874. return 0;
  2875. }
  2876. int i915_gem_get_caching_ioctl(struct drm_device *dev, void *data,
  2877. struct drm_file *file)
  2878. {
  2879. struct drm_i915_gem_caching *args = data;
  2880. struct drm_i915_gem_object *obj;
  2881. obj = i915_gem_object_lookup(file, args->handle);
  2882. if (!obj)
  2883. return -ENOENT;
  2884. switch (obj->cache_level) {
  2885. case I915_CACHE_LLC:
  2886. case I915_CACHE_L3_LLC:
  2887. args->caching = I915_CACHING_CACHED;
  2888. break;
  2889. case I915_CACHE_WT:
  2890. args->caching = I915_CACHING_DISPLAY;
  2891. break;
  2892. default:
  2893. args->caching = I915_CACHING_NONE;
  2894. break;
  2895. }
  2896. i915_gem_object_put_unlocked(obj);
  2897. return 0;
  2898. }
  2899. int i915_gem_set_caching_ioctl(struct drm_device *dev, void *data,
  2900. struct drm_file *file)
  2901. {
  2902. struct drm_i915_private *dev_priv = to_i915(dev);
  2903. struct drm_i915_gem_caching *args = data;
  2904. struct drm_i915_gem_object *obj;
  2905. enum i915_cache_level level;
  2906. int ret;
  2907. switch (args->caching) {
  2908. case I915_CACHING_NONE:
  2909. level = I915_CACHE_NONE;
  2910. break;
  2911. case I915_CACHING_CACHED:
  2912. /*
  2913. * Due to a HW issue on BXT A stepping, GPU stores via a
  2914. * snooped mapping may leave stale data in a corresponding CPU
  2915. * cacheline, whereas normally such cachelines would get
  2916. * invalidated.
  2917. */
  2918. if (!HAS_LLC(dev) && !HAS_SNOOP(dev))
  2919. return -ENODEV;
  2920. level = I915_CACHE_LLC;
  2921. break;
  2922. case I915_CACHING_DISPLAY:
  2923. level = HAS_WT(dev) ? I915_CACHE_WT : I915_CACHE_NONE;
  2924. break;
  2925. default:
  2926. return -EINVAL;
  2927. }
  2928. intel_runtime_pm_get(dev_priv);
  2929. ret = i915_mutex_lock_interruptible(dev);
  2930. if (ret)
  2931. goto rpm_put;
  2932. obj = i915_gem_object_lookup(file, args->handle);
  2933. if (!obj) {
  2934. ret = -ENOENT;
  2935. goto unlock;
  2936. }
  2937. ret = i915_gem_object_set_cache_level(obj, level);
  2938. i915_gem_object_put(obj);
  2939. unlock:
  2940. mutex_unlock(&dev->struct_mutex);
  2941. rpm_put:
  2942. intel_runtime_pm_put(dev_priv);
  2943. return ret;
  2944. }
  2945. /*
  2946. * Prepare buffer for display plane (scanout, cursors, etc).
  2947. * Can be called from an uninterruptible phase (modesetting) and allows
  2948. * any flushes to be pipelined (for pageflips).
  2949. */
  2950. struct i915_vma *
  2951. i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj,
  2952. u32 alignment,
  2953. const struct i915_ggtt_view *view)
  2954. {
  2955. struct i915_vma *vma;
  2956. u32 old_read_domains, old_write_domain;
  2957. int ret;
  2958. /* Mark the pin_display early so that we account for the
  2959. * display coherency whilst setting up the cache domains.
  2960. */
  2961. obj->pin_display++;
  2962. /* The display engine is not coherent with the LLC cache on gen6. As
  2963. * a result, we make sure that the pinning that is about to occur is
  2964. * done with uncached PTEs. This is lowest common denominator for all
  2965. * chipsets.
  2966. *
  2967. * However for gen6+, we could do better by using the GFDT bit instead
  2968. * of uncaching, which would allow us to flush all the LLC-cached data
  2969. * with that bit in the PTE to main memory with just one PIPE_CONTROL.
  2970. */
  2971. ret = i915_gem_object_set_cache_level(obj,
  2972. HAS_WT(obj->base.dev) ? I915_CACHE_WT : I915_CACHE_NONE);
  2973. if (ret) {
  2974. vma = ERR_PTR(ret);
  2975. goto err_unpin_display;
  2976. }
  2977. /* As the user may map the buffer once pinned in the display plane
  2978. * (e.g. libkms for the bootup splash), we have to ensure that we
  2979. * always use map_and_fenceable for all scanout buffers. However,
  2980. * it may simply be too big to fit into mappable, in which case
  2981. * put it anyway and hope that userspace can cope (but always first
  2982. * try to preserve the existing ABI).
  2983. */
  2984. vma = ERR_PTR(-ENOSPC);
  2985. if (view->type == I915_GGTT_VIEW_NORMAL)
  2986. vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment,
  2987. PIN_MAPPABLE | PIN_NONBLOCK);
  2988. if (IS_ERR(vma)) {
  2989. struct drm_i915_private *i915 = to_i915(obj->base.dev);
  2990. unsigned int flags;
  2991. /* Valleyview is definitely limited to scanning out the first
  2992. * 512MiB. Lets presume this behaviour was inherited from the
  2993. * g4x display engine and that all earlier gen are similarly
  2994. * limited. Testing suggests that it is a little more
  2995. * complicated than this. For example, Cherryview appears quite
  2996. * happy to scanout from anywhere within its global aperture.
  2997. */
  2998. flags = 0;
  2999. if (HAS_GMCH_DISPLAY(i915))
  3000. flags = PIN_MAPPABLE;
  3001. vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment, flags);
  3002. }
  3003. if (IS_ERR(vma))
  3004. goto err_unpin_display;
  3005. vma->display_alignment = max_t(u64, vma->display_alignment, alignment);
  3006. i915_gem_object_flush_cpu_write_domain(obj);
  3007. old_write_domain = obj->base.write_domain;
  3008. old_read_domains = obj->base.read_domains;
  3009. /* It should now be out of any other write domains, and we can update
  3010. * the domain values for our changes.
  3011. */
  3012. obj->base.write_domain = 0;
  3013. obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
  3014. trace_i915_gem_object_change_domain(obj,
  3015. old_read_domains,
  3016. old_write_domain);
  3017. return vma;
  3018. err_unpin_display:
  3019. obj->pin_display--;
  3020. return vma;
  3021. }
  3022. void
  3023. i915_gem_object_unpin_from_display_plane(struct i915_vma *vma)
  3024. {
  3025. if (WARN_ON(vma->obj->pin_display == 0))
  3026. return;
  3027. if (--vma->obj->pin_display == 0)
  3028. vma->display_alignment = 0;
  3029. /* Bump the LRU to try and avoid premature eviction whilst flipping */
  3030. if (!i915_vma_is_active(vma))
  3031. list_move_tail(&vma->vm_link, &vma->vm->inactive_list);
  3032. i915_vma_unpin(vma);
  3033. }
  3034. /**
  3035. * Moves a single object to the CPU read, and possibly write domain.
  3036. * @obj: object to act on
  3037. * @write: requesting write or read-only access
  3038. *
  3039. * This function returns when the move is complete, including waiting on
  3040. * flushes to occur.
  3041. */
  3042. int
  3043. i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write)
  3044. {
  3045. uint32_t old_write_domain, old_read_domains;
  3046. int ret;
  3047. ret = i915_gem_object_wait_rendering(obj, !write);
  3048. if (ret)
  3049. return ret;
  3050. if (obj->base.write_domain == I915_GEM_DOMAIN_CPU)
  3051. return 0;
  3052. i915_gem_object_flush_gtt_write_domain(obj);
  3053. old_write_domain = obj->base.write_domain;
  3054. old_read_domains = obj->base.read_domains;
  3055. /* Flush the CPU cache if it's still invalid. */
  3056. if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0) {
  3057. i915_gem_clflush_object(obj, false);
  3058. obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
  3059. }
  3060. /* It should now be out of any other write domains, and we can update
  3061. * the domain values for our changes.
  3062. */
  3063. BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
  3064. /* If we're writing through the CPU, then the GPU read domains will
  3065. * need to be invalidated at next use.
  3066. */
  3067. if (write) {
  3068. obj->base.read_domains = I915_GEM_DOMAIN_CPU;
  3069. obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  3070. }
  3071. trace_i915_gem_object_change_domain(obj,
  3072. old_read_domains,
  3073. old_write_domain);
  3074. return 0;
  3075. }
  3076. /* Throttle our rendering by waiting until the ring has completed our requests
  3077. * emitted over 20 msec ago.
  3078. *
  3079. * Note that if we were to use the current jiffies each time around the loop,
  3080. * we wouldn't escape the function with any frames outstanding if the time to
  3081. * render a frame was over 20ms.
  3082. *
  3083. * This should get us reasonable parallelism between CPU and GPU but also
  3084. * relatively low latency when blocking on a particular request to finish.
  3085. */
  3086. static int
  3087. i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
  3088. {
  3089. struct drm_i915_private *dev_priv = to_i915(dev);
  3090. struct drm_i915_file_private *file_priv = file->driver_priv;
  3091. unsigned long recent_enough = jiffies - DRM_I915_THROTTLE_JIFFIES;
  3092. struct drm_i915_gem_request *request, *target = NULL;
  3093. int ret;
  3094. ret = i915_gem_wait_for_error(&dev_priv->gpu_error);
  3095. if (ret)
  3096. return ret;
  3097. /* ABI: return -EIO if already wedged */
  3098. if (i915_terminally_wedged(&dev_priv->gpu_error))
  3099. return -EIO;
  3100. spin_lock(&file_priv->mm.lock);
  3101. list_for_each_entry(request, &file_priv->mm.request_list, client_list) {
  3102. if (time_after_eq(request->emitted_jiffies, recent_enough))
  3103. break;
  3104. /*
  3105. * Note that the request might not have been submitted yet.
  3106. * In which case emitted_jiffies will be zero.
  3107. */
  3108. if (!request->emitted_jiffies)
  3109. continue;
  3110. target = request;
  3111. }
  3112. if (target)
  3113. i915_gem_request_get(target);
  3114. spin_unlock(&file_priv->mm.lock);
  3115. if (target == NULL)
  3116. return 0;
  3117. ret = i915_wait_request(target, I915_WAIT_INTERRUPTIBLE, NULL, NULL);
  3118. i915_gem_request_put(target);
  3119. return ret;
  3120. }
  3121. static bool
  3122. i915_vma_misplaced(struct i915_vma *vma, u64 size, u64 alignment, u64 flags)
  3123. {
  3124. if (!drm_mm_node_allocated(&vma->node))
  3125. return false;
  3126. if (vma->node.size < size)
  3127. return true;
  3128. if (alignment && vma->node.start & (alignment - 1))
  3129. return true;
  3130. if (flags & PIN_MAPPABLE && !i915_vma_is_map_and_fenceable(vma))
  3131. return true;
  3132. if (flags & PIN_OFFSET_BIAS &&
  3133. vma->node.start < (flags & PIN_OFFSET_MASK))
  3134. return true;
  3135. if (flags & PIN_OFFSET_FIXED &&
  3136. vma->node.start != (flags & PIN_OFFSET_MASK))
  3137. return true;
  3138. return false;
  3139. }
  3140. void __i915_vma_set_map_and_fenceable(struct i915_vma *vma)
  3141. {
  3142. struct drm_i915_gem_object *obj = vma->obj;
  3143. struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
  3144. bool mappable, fenceable;
  3145. u32 fence_size, fence_alignment;
  3146. fence_size = i915_gem_get_ggtt_size(dev_priv,
  3147. vma->size,
  3148. i915_gem_object_get_tiling(obj));
  3149. fence_alignment = i915_gem_get_ggtt_alignment(dev_priv,
  3150. vma->size,
  3151. i915_gem_object_get_tiling(obj),
  3152. true);
  3153. fenceable = (vma->node.size == fence_size &&
  3154. (vma->node.start & (fence_alignment - 1)) == 0);
  3155. mappable = (vma->node.start + fence_size <=
  3156. dev_priv->ggtt.mappable_end);
  3157. /*
  3158. * Explicitly disable for rotated VMA since the display does not
  3159. * need the fence and the VMA is not accessible to other users.
  3160. */
  3161. if (mappable && fenceable &&
  3162. vma->ggtt_view.type != I915_GGTT_VIEW_ROTATED)
  3163. vma->flags |= I915_VMA_CAN_FENCE;
  3164. else
  3165. vma->flags &= ~I915_VMA_CAN_FENCE;
  3166. }
  3167. int __i915_vma_do_pin(struct i915_vma *vma,
  3168. u64 size, u64 alignment, u64 flags)
  3169. {
  3170. unsigned int bound = vma->flags;
  3171. int ret;
  3172. GEM_BUG_ON((flags & (PIN_GLOBAL | PIN_USER)) == 0);
  3173. GEM_BUG_ON((flags & PIN_GLOBAL) && !i915_vma_is_ggtt(vma));
  3174. if (WARN_ON(bound & I915_VMA_PIN_OVERFLOW)) {
  3175. ret = -EBUSY;
  3176. goto err;
  3177. }
  3178. if ((bound & I915_VMA_BIND_MASK) == 0) {
  3179. ret = i915_vma_insert(vma, size, alignment, flags);
  3180. if (ret)
  3181. goto err;
  3182. }
  3183. ret = i915_vma_bind(vma, vma->obj->cache_level, flags);
  3184. if (ret)
  3185. goto err;
  3186. if ((bound ^ vma->flags) & I915_VMA_GLOBAL_BIND)
  3187. __i915_vma_set_map_and_fenceable(vma);
  3188. GEM_BUG_ON(i915_vma_misplaced(vma, size, alignment, flags));
  3189. return 0;
  3190. err:
  3191. __i915_vma_unpin(vma);
  3192. return ret;
  3193. }
  3194. struct i915_vma *
  3195. i915_gem_object_ggtt_pin(struct drm_i915_gem_object *obj,
  3196. const struct i915_ggtt_view *view,
  3197. u64 size,
  3198. u64 alignment,
  3199. u64 flags)
  3200. {
  3201. struct i915_address_space *vm = &to_i915(obj->base.dev)->ggtt.base;
  3202. struct i915_vma *vma;
  3203. int ret;
  3204. vma = i915_gem_obj_lookup_or_create_vma(obj, vm, view);
  3205. if (IS_ERR(vma))
  3206. return vma;
  3207. if (i915_vma_misplaced(vma, size, alignment, flags)) {
  3208. if (flags & PIN_NONBLOCK &&
  3209. (i915_vma_is_pinned(vma) || i915_vma_is_active(vma)))
  3210. return ERR_PTR(-ENOSPC);
  3211. WARN(i915_vma_is_pinned(vma),
  3212. "bo is already pinned in ggtt with incorrect alignment:"
  3213. " offset=%08x, req.alignment=%llx,"
  3214. " req.map_and_fenceable=%d, vma->map_and_fenceable=%d\n",
  3215. i915_ggtt_offset(vma), alignment,
  3216. !!(flags & PIN_MAPPABLE),
  3217. i915_vma_is_map_and_fenceable(vma));
  3218. ret = i915_vma_unbind(vma);
  3219. if (ret)
  3220. return ERR_PTR(ret);
  3221. }
  3222. ret = i915_vma_pin(vma, size, alignment, flags | PIN_GLOBAL);
  3223. if (ret)
  3224. return ERR_PTR(ret);
  3225. return vma;
  3226. }
  3227. static __always_inline unsigned int __busy_read_flag(unsigned int id)
  3228. {
  3229. /* Note that we could alias engines in the execbuf API, but
  3230. * that would be very unwise as it prevents userspace from
  3231. * fine control over engine selection. Ahem.
  3232. *
  3233. * This should be something like EXEC_MAX_ENGINE instead of
  3234. * I915_NUM_ENGINES.
  3235. */
  3236. BUILD_BUG_ON(I915_NUM_ENGINES > 16);
  3237. return 0x10000 << id;
  3238. }
  3239. static __always_inline unsigned int __busy_write_id(unsigned int id)
  3240. {
  3241. /* The uABI guarantees an active writer is also amongst the read
  3242. * engines. This would be true if we accessed the activity tracking
  3243. * under the lock, but as we perform the lookup of the object and
  3244. * its activity locklessly we can not guarantee that the last_write
  3245. * being active implies that we have set the same engine flag from
  3246. * last_read - hence we always set both read and write busy for
  3247. * last_write.
  3248. */
  3249. return id | __busy_read_flag(id);
  3250. }
  3251. static __always_inline unsigned int
  3252. __busy_set_if_active(const struct i915_gem_active *active,
  3253. unsigned int (*flag)(unsigned int id))
  3254. {
  3255. struct drm_i915_gem_request *request;
  3256. request = rcu_dereference(active->request);
  3257. if (!request || i915_gem_request_completed(request))
  3258. return 0;
  3259. /* This is racy. See __i915_gem_active_get_rcu() for an in detail
  3260. * discussion of how to handle the race correctly, but for reporting
  3261. * the busy state we err on the side of potentially reporting the
  3262. * wrong engine as being busy (but we guarantee that the result
  3263. * is at least self-consistent).
  3264. *
  3265. * As we use SLAB_DESTROY_BY_RCU, the request may be reallocated
  3266. * whilst we are inspecting it, even under the RCU read lock as we are.
  3267. * This means that there is a small window for the engine and/or the
  3268. * seqno to have been overwritten. The seqno will always be in the
  3269. * future compared to the intended, and so we know that if that
  3270. * seqno is idle (on whatever engine) our request is idle and the
  3271. * return 0 above is correct.
  3272. *
  3273. * The issue is that if the engine is switched, it is just as likely
  3274. * to report that it is busy (but since the switch happened, we know
  3275. * the request should be idle). So there is a small chance that a busy
  3276. * result is actually the wrong engine.
  3277. *
  3278. * So why don't we care?
  3279. *
  3280. * For starters, the busy ioctl is a heuristic that is by definition
  3281. * racy. Even with perfect serialisation in the driver, the hardware
  3282. * state is constantly advancing - the state we report to the user
  3283. * is stale.
  3284. *
  3285. * The critical information for the busy-ioctl is whether the object
  3286. * is idle as userspace relies on that to detect whether its next
  3287. * access will stall, or if it has missed submitting commands to
  3288. * the hardware allowing the GPU to stall. We never generate a
  3289. * false-positive for idleness, thus busy-ioctl is reliable at the
  3290. * most fundamental level, and we maintain the guarantee that a
  3291. * busy object left to itself will eventually become idle (and stay
  3292. * idle!).
  3293. *
  3294. * We allow ourselves the leeway of potentially misreporting the busy
  3295. * state because that is an optimisation heuristic that is constantly
  3296. * in flux. Being quickly able to detect the busy/idle state is much
  3297. * more important than accurate logging of exactly which engines were
  3298. * busy.
  3299. *
  3300. * For accuracy in reporting the engine, we could use
  3301. *
  3302. * result = 0;
  3303. * request = __i915_gem_active_get_rcu(active);
  3304. * if (request) {
  3305. * if (!i915_gem_request_completed(request))
  3306. * result = flag(request->engine->exec_id);
  3307. * i915_gem_request_put(request);
  3308. * }
  3309. *
  3310. * but that still remains susceptible to both hardware and userspace
  3311. * races. So we accept making the result of that race slightly worse,
  3312. * given the rarity of the race and its low impact on the result.
  3313. */
  3314. return flag(READ_ONCE(request->engine->exec_id));
  3315. }
  3316. static __always_inline unsigned int
  3317. busy_check_reader(const struct i915_gem_active *active)
  3318. {
  3319. return __busy_set_if_active(active, __busy_read_flag);
  3320. }
  3321. static __always_inline unsigned int
  3322. busy_check_writer(const struct i915_gem_active *active)
  3323. {
  3324. return __busy_set_if_active(active, __busy_write_id);
  3325. }
  3326. int
  3327. i915_gem_busy_ioctl(struct drm_device *dev, void *data,
  3328. struct drm_file *file)
  3329. {
  3330. struct drm_i915_gem_busy *args = data;
  3331. struct drm_i915_gem_object *obj;
  3332. unsigned long active;
  3333. obj = i915_gem_object_lookup(file, args->handle);
  3334. if (!obj)
  3335. return -ENOENT;
  3336. args->busy = 0;
  3337. active = __I915_BO_ACTIVE(obj);
  3338. if (active) {
  3339. int idx;
  3340. /* Yes, the lookups are intentionally racy.
  3341. *
  3342. * First, we cannot simply rely on __I915_BO_ACTIVE. We have
  3343. * to regard the value as stale and as our ABI guarantees
  3344. * forward progress, we confirm the status of each active
  3345. * request with the hardware.
  3346. *
  3347. * Even though we guard the pointer lookup by RCU, that only
  3348. * guarantees that the pointer and its contents remain
  3349. * dereferencable and does *not* mean that the request we
  3350. * have is the same as the one being tracked by the object.
  3351. *
  3352. * Consider that we lookup the request just as it is being
  3353. * retired and freed. We take a local copy of the pointer,
  3354. * but before we add its engine into the busy set, the other
  3355. * thread reallocates it and assigns it to a task on another
  3356. * engine with a fresh and incomplete seqno. Guarding against
  3357. * that requires careful serialisation and reference counting,
  3358. * i.e. using __i915_gem_active_get_request_rcu(). We don't,
  3359. * instead we expect that if the result is busy, which engines
  3360. * are busy is not completely reliable - we only guarantee
  3361. * that the object was busy.
  3362. */
  3363. rcu_read_lock();
  3364. for_each_active(active, idx)
  3365. args->busy |= busy_check_reader(&obj->last_read[idx]);
  3366. /* For ABI sanity, we only care that the write engine is in
  3367. * the set of read engines. This should be ensured by the
  3368. * ordering of setting last_read/last_write in
  3369. * i915_vma_move_to_active(), and then in reverse in retire.
  3370. * However, for good measure, we always report the last_write
  3371. * request as a busy read as well as being a busy write.
  3372. *
  3373. * We don't care that the set of active read/write engines
  3374. * may change during construction of the result, as it is
  3375. * equally liable to change before userspace can inspect
  3376. * the result.
  3377. */
  3378. args->busy |= busy_check_writer(&obj->last_write);
  3379. rcu_read_unlock();
  3380. }
  3381. i915_gem_object_put_unlocked(obj);
  3382. return 0;
  3383. }
  3384. int
  3385. i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
  3386. struct drm_file *file_priv)
  3387. {
  3388. return i915_gem_ring_throttle(dev, file_priv);
  3389. }
  3390. int
  3391. i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
  3392. struct drm_file *file_priv)
  3393. {
  3394. struct drm_i915_private *dev_priv = to_i915(dev);
  3395. struct drm_i915_gem_madvise *args = data;
  3396. struct drm_i915_gem_object *obj;
  3397. int ret;
  3398. switch (args->madv) {
  3399. case I915_MADV_DONTNEED:
  3400. case I915_MADV_WILLNEED:
  3401. break;
  3402. default:
  3403. return -EINVAL;
  3404. }
  3405. ret = i915_mutex_lock_interruptible(dev);
  3406. if (ret)
  3407. return ret;
  3408. obj = i915_gem_object_lookup(file_priv, args->handle);
  3409. if (!obj) {
  3410. ret = -ENOENT;
  3411. goto unlock;
  3412. }
  3413. if (obj->pages &&
  3414. i915_gem_object_is_tiled(obj) &&
  3415. dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
  3416. if (obj->madv == I915_MADV_WILLNEED)
  3417. i915_gem_object_unpin_pages(obj);
  3418. if (args->madv == I915_MADV_WILLNEED)
  3419. i915_gem_object_pin_pages(obj);
  3420. }
  3421. if (obj->madv != __I915_MADV_PURGED)
  3422. obj->madv = args->madv;
  3423. /* if the object is no longer attached, discard its backing storage */
  3424. if (obj->madv == I915_MADV_DONTNEED && obj->pages == NULL)
  3425. i915_gem_object_truncate(obj);
  3426. args->retained = obj->madv != __I915_MADV_PURGED;
  3427. i915_gem_object_put(obj);
  3428. unlock:
  3429. mutex_unlock(&dev->struct_mutex);
  3430. return ret;
  3431. }
  3432. void i915_gem_object_init(struct drm_i915_gem_object *obj,
  3433. const struct drm_i915_gem_object_ops *ops)
  3434. {
  3435. int i;
  3436. INIT_LIST_HEAD(&obj->global_list);
  3437. for (i = 0; i < I915_NUM_ENGINES; i++)
  3438. init_request_active(&obj->last_read[i],
  3439. i915_gem_object_retire__read);
  3440. init_request_active(&obj->last_write,
  3441. i915_gem_object_retire__write);
  3442. INIT_LIST_HEAD(&obj->obj_exec_link);
  3443. INIT_LIST_HEAD(&obj->vma_list);
  3444. INIT_LIST_HEAD(&obj->batch_pool_link);
  3445. obj->ops = ops;
  3446. obj->frontbuffer_ggtt_origin = ORIGIN_GTT;
  3447. obj->madv = I915_MADV_WILLNEED;
  3448. i915_gem_info_add_obj(to_i915(obj->base.dev), obj->base.size);
  3449. }
  3450. static const struct drm_i915_gem_object_ops i915_gem_object_ops = {
  3451. .flags = I915_GEM_OBJECT_HAS_STRUCT_PAGE,
  3452. .get_pages = i915_gem_object_get_pages_gtt,
  3453. .put_pages = i915_gem_object_put_pages_gtt,
  3454. };
  3455. struct drm_i915_gem_object *i915_gem_object_create(struct drm_device *dev,
  3456. size_t size)
  3457. {
  3458. struct drm_i915_gem_object *obj;
  3459. struct address_space *mapping;
  3460. gfp_t mask;
  3461. int ret;
  3462. obj = i915_gem_object_alloc(dev);
  3463. if (obj == NULL)
  3464. return ERR_PTR(-ENOMEM);
  3465. ret = drm_gem_object_init(dev, &obj->base, size);
  3466. if (ret)
  3467. goto fail;
  3468. mask = GFP_HIGHUSER | __GFP_RECLAIMABLE;
  3469. if (IS_CRESTLINE(dev) || IS_BROADWATER(dev)) {
  3470. /* 965gm cannot relocate objects above 4GiB. */
  3471. mask &= ~__GFP_HIGHMEM;
  3472. mask |= __GFP_DMA32;
  3473. }
  3474. mapping = obj->base.filp->f_mapping;
  3475. mapping_set_gfp_mask(mapping, mask);
  3476. i915_gem_object_init(obj, &i915_gem_object_ops);
  3477. obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  3478. obj->base.read_domains = I915_GEM_DOMAIN_CPU;
  3479. if (HAS_LLC(dev)) {
  3480. /* On some devices, we can have the GPU use the LLC (the CPU
  3481. * cache) for about a 10% performance improvement
  3482. * compared to uncached. Graphics requests other than
  3483. * display scanout are coherent with the CPU in
  3484. * accessing this cache. This means in this mode we
  3485. * don't need to clflush on the CPU side, and on the
  3486. * GPU side we only need to flush internal caches to
  3487. * get data visible to the CPU.
  3488. *
  3489. * However, we maintain the display planes as UC, and so
  3490. * need to rebind when first used as such.
  3491. */
  3492. obj->cache_level = I915_CACHE_LLC;
  3493. } else
  3494. obj->cache_level = I915_CACHE_NONE;
  3495. trace_i915_gem_object_create(obj);
  3496. return obj;
  3497. fail:
  3498. i915_gem_object_free(obj);
  3499. return ERR_PTR(ret);
  3500. }
  3501. static bool discard_backing_storage(struct drm_i915_gem_object *obj)
  3502. {
  3503. /* If we are the last user of the backing storage (be it shmemfs
  3504. * pages or stolen etc), we know that the pages are going to be
  3505. * immediately released. In this case, we can then skip copying
  3506. * back the contents from the GPU.
  3507. */
  3508. if (obj->madv != I915_MADV_WILLNEED)
  3509. return false;
  3510. if (obj->base.filp == NULL)
  3511. return true;
  3512. /* At first glance, this looks racy, but then again so would be
  3513. * userspace racing mmap against close. However, the first external
  3514. * reference to the filp can only be obtained through the
  3515. * i915_gem_mmap_ioctl() which safeguards us against the user
  3516. * acquiring such a reference whilst we are in the middle of
  3517. * freeing the object.
  3518. */
  3519. return atomic_long_read(&obj->base.filp->f_count) == 1;
  3520. }
  3521. void i915_gem_free_object(struct drm_gem_object *gem_obj)
  3522. {
  3523. struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
  3524. struct drm_device *dev = obj->base.dev;
  3525. struct drm_i915_private *dev_priv = to_i915(dev);
  3526. struct i915_vma *vma, *next;
  3527. intel_runtime_pm_get(dev_priv);
  3528. trace_i915_gem_object_destroy(obj);
  3529. /* All file-owned VMA should have been released by this point through
  3530. * i915_gem_close_object(), or earlier by i915_gem_context_close().
  3531. * However, the object may also be bound into the global GTT (e.g.
  3532. * older GPUs without per-process support, or for direct access through
  3533. * the GTT either for the user or for scanout). Those VMA still need to
  3534. * unbound now.
  3535. */
  3536. list_for_each_entry_safe(vma, next, &obj->vma_list, obj_link) {
  3537. GEM_BUG_ON(!i915_vma_is_ggtt(vma));
  3538. GEM_BUG_ON(i915_vma_is_active(vma));
  3539. vma->flags &= ~I915_VMA_PIN_MASK;
  3540. i915_vma_close(vma);
  3541. }
  3542. GEM_BUG_ON(obj->bind_count);
  3543. /* Stolen objects don't hold a ref, but do hold pin count. Fix that up
  3544. * before progressing. */
  3545. if (obj->stolen)
  3546. i915_gem_object_unpin_pages(obj);
  3547. WARN_ON(atomic_read(&obj->frontbuffer_bits));
  3548. if (obj->pages && obj->madv == I915_MADV_WILLNEED &&
  3549. dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES &&
  3550. i915_gem_object_is_tiled(obj))
  3551. i915_gem_object_unpin_pages(obj);
  3552. if (WARN_ON(obj->pages_pin_count))
  3553. obj->pages_pin_count = 0;
  3554. if (discard_backing_storage(obj))
  3555. obj->madv = I915_MADV_DONTNEED;
  3556. i915_gem_object_put_pages(obj);
  3557. BUG_ON(obj->pages);
  3558. if (obj->base.import_attach)
  3559. drm_prime_gem_destroy(&obj->base, NULL);
  3560. if (obj->ops->release)
  3561. obj->ops->release(obj);
  3562. drm_gem_object_release(&obj->base);
  3563. i915_gem_info_remove_obj(dev_priv, obj->base.size);
  3564. kfree(obj->bit_17);
  3565. i915_gem_object_free(obj);
  3566. intel_runtime_pm_put(dev_priv);
  3567. }
  3568. int i915_gem_suspend(struct drm_device *dev)
  3569. {
  3570. struct drm_i915_private *dev_priv = to_i915(dev);
  3571. int ret;
  3572. intel_suspend_gt_powersave(dev_priv);
  3573. mutex_lock(&dev->struct_mutex);
  3574. /* We have to flush all the executing contexts to main memory so
  3575. * that they can saved in the hibernation image. To ensure the last
  3576. * context image is coherent, we have to switch away from it. That
  3577. * leaves the dev_priv->kernel_context still active when
  3578. * we actually suspend, and its image in memory may not match the GPU
  3579. * state. Fortunately, the kernel_context is disposable and we do
  3580. * not rely on its state.
  3581. */
  3582. ret = i915_gem_switch_to_kernel_context(dev_priv);
  3583. if (ret)
  3584. goto err;
  3585. ret = i915_gem_wait_for_idle(dev_priv,
  3586. I915_WAIT_INTERRUPTIBLE |
  3587. I915_WAIT_LOCKED);
  3588. if (ret)
  3589. goto err;
  3590. i915_gem_retire_requests(dev_priv);
  3591. i915_gem_context_lost(dev_priv);
  3592. mutex_unlock(&dev->struct_mutex);
  3593. cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work);
  3594. cancel_delayed_work_sync(&dev_priv->gt.retire_work);
  3595. flush_delayed_work(&dev_priv->gt.idle_work);
  3596. /* Assert that we sucessfully flushed all the work and
  3597. * reset the GPU back to its idle, low power state.
  3598. */
  3599. WARN_ON(dev_priv->gt.awake);
  3600. return 0;
  3601. err:
  3602. mutex_unlock(&dev->struct_mutex);
  3603. return ret;
  3604. }
  3605. void i915_gem_resume(struct drm_device *dev)
  3606. {
  3607. struct drm_i915_private *dev_priv = to_i915(dev);
  3608. mutex_lock(&dev->struct_mutex);
  3609. i915_gem_restore_gtt_mappings(dev);
  3610. /* As we didn't flush the kernel context before suspend, we cannot
  3611. * guarantee that the context image is complete. So let's just reset
  3612. * it and start again.
  3613. */
  3614. dev_priv->gt.resume(dev_priv);
  3615. mutex_unlock(&dev->struct_mutex);
  3616. }
  3617. void i915_gem_init_swizzling(struct drm_device *dev)
  3618. {
  3619. struct drm_i915_private *dev_priv = to_i915(dev);
  3620. if (INTEL_INFO(dev)->gen < 5 ||
  3621. dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE)
  3622. return;
  3623. I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
  3624. DISP_TILE_SURFACE_SWIZZLING);
  3625. if (IS_GEN5(dev))
  3626. return;
  3627. I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL);
  3628. if (IS_GEN6(dev))
  3629. I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_SNB));
  3630. else if (IS_GEN7(dev))
  3631. I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_IVB));
  3632. else if (IS_GEN8(dev))
  3633. I915_WRITE(GAMTARBMODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_BDW));
  3634. else
  3635. BUG();
  3636. }
  3637. static void init_unused_ring(struct drm_device *dev, u32 base)
  3638. {
  3639. struct drm_i915_private *dev_priv = to_i915(dev);
  3640. I915_WRITE(RING_CTL(base), 0);
  3641. I915_WRITE(RING_HEAD(base), 0);
  3642. I915_WRITE(RING_TAIL(base), 0);
  3643. I915_WRITE(RING_START(base), 0);
  3644. }
  3645. static void init_unused_rings(struct drm_device *dev)
  3646. {
  3647. if (IS_I830(dev)) {
  3648. init_unused_ring(dev, PRB1_BASE);
  3649. init_unused_ring(dev, SRB0_BASE);
  3650. init_unused_ring(dev, SRB1_BASE);
  3651. init_unused_ring(dev, SRB2_BASE);
  3652. init_unused_ring(dev, SRB3_BASE);
  3653. } else if (IS_GEN2(dev)) {
  3654. init_unused_ring(dev, SRB0_BASE);
  3655. init_unused_ring(dev, SRB1_BASE);
  3656. } else if (IS_GEN3(dev)) {
  3657. init_unused_ring(dev, PRB1_BASE);
  3658. init_unused_ring(dev, PRB2_BASE);
  3659. }
  3660. }
  3661. int
  3662. i915_gem_init_hw(struct drm_device *dev)
  3663. {
  3664. struct drm_i915_private *dev_priv = to_i915(dev);
  3665. struct intel_engine_cs *engine;
  3666. int ret;
  3667. /* Double layer security blanket, see i915_gem_init() */
  3668. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  3669. if (HAS_EDRAM(dev) && INTEL_GEN(dev_priv) < 9)
  3670. I915_WRITE(HSW_IDICR, I915_READ(HSW_IDICR) | IDIHASHMSK(0xf));
  3671. if (IS_HASWELL(dev))
  3672. I915_WRITE(MI_PREDICATE_RESULT_2, IS_HSW_GT3(dev) ?
  3673. LOWER_SLICE_ENABLED : LOWER_SLICE_DISABLED);
  3674. if (HAS_PCH_NOP(dev)) {
  3675. if (IS_IVYBRIDGE(dev)) {
  3676. u32 temp = I915_READ(GEN7_MSG_CTL);
  3677. temp &= ~(WAIT_FOR_PCH_FLR_ACK | WAIT_FOR_PCH_RESET_ACK);
  3678. I915_WRITE(GEN7_MSG_CTL, temp);
  3679. } else if (INTEL_INFO(dev)->gen >= 7) {
  3680. u32 temp = I915_READ(HSW_NDE_RSTWRN_OPT);
  3681. temp &= ~RESET_PCH_HANDSHAKE_ENABLE;
  3682. I915_WRITE(HSW_NDE_RSTWRN_OPT, temp);
  3683. }
  3684. }
  3685. i915_gem_init_swizzling(dev);
  3686. /*
  3687. * At least 830 can leave some of the unused rings
  3688. * "active" (ie. head != tail) after resume which
  3689. * will prevent c3 entry. Makes sure all unused rings
  3690. * are totally idle.
  3691. */
  3692. init_unused_rings(dev);
  3693. BUG_ON(!dev_priv->kernel_context);
  3694. ret = i915_ppgtt_init_hw(dev);
  3695. if (ret) {
  3696. DRM_ERROR("PPGTT enable HW failed %d\n", ret);
  3697. goto out;
  3698. }
  3699. /* Need to do basic initialisation of all rings first: */
  3700. for_each_engine(engine, dev_priv) {
  3701. ret = engine->init_hw(engine);
  3702. if (ret)
  3703. goto out;
  3704. }
  3705. intel_mocs_init_l3cc_table(dev);
  3706. /* We can't enable contexts until all firmware is loaded */
  3707. ret = intel_guc_setup(dev);
  3708. if (ret)
  3709. goto out;
  3710. out:
  3711. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  3712. return ret;
  3713. }
  3714. bool intel_sanitize_semaphores(struct drm_i915_private *dev_priv, int value)
  3715. {
  3716. if (INTEL_INFO(dev_priv)->gen < 6)
  3717. return false;
  3718. /* TODO: make semaphores and Execlists play nicely together */
  3719. if (i915.enable_execlists)
  3720. return false;
  3721. if (value >= 0)
  3722. return value;
  3723. #ifdef CONFIG_INTEL_IOMMU
  3724. /* Enable semaphores on SNB when IO remapping is off */
  3725. if (INTEL_INFO(dev_priv)->gen == 6 && intel_iommu_gfx_mapped)
  3726. return false;
  3727. #endif
  3728. return true;
  3729. }
  3730. int i915_gem_init(struct drm_device *dev)
  3731. {
  3732. struct drm_i915_private *dev_priv = to_i915(dev);
  3733. int ret;
  3734. mutex_lock(&dev->struct_mutex);
  3735. if (!i915.enable_execlists) {
  3736. dev_priv->gt.resume = intel_legacy_submission_resume;
  3737. dev_priv->gt.cleanup_engine = intel_engine_cleanup;
  3738. } else {
  3739. dev_priv->gt.resume = intel_lr_context_resume;
  3740. dev_priv->gt.cleanup_engine = intel_logical_ring_cleanup;
  3741. }
  3742. /* This is just a security blanket to placate dragons.
  3743. * On some systems, we very sporadically observe that the first TLBs
  3744. * used by the CS may be stale, despite us poking the TLB reset. If
  3745. * we hold the forcewake during initialisation these problems
  3746. * just magically go away.
  3747. */
  3748. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  3749. i915_gem_init_userptr(dev_priv);
  3750. ret = i915_gem_init_ggtt(dev_priv);
  3751. if (ret)
  3752. goto out_unlock;
  3753. ret = i915_gem_context_init(dev);
  3754. if (ret)
  3755. goto out_unlock;
  3756. ret = intel_engines_init(dev);
  3757. if (ret)
  3758. goto out_unlock;
  3759. ret = i915_gem_init_hw(dev);
  3760. if (ret == -EIO) {
  3761. /* Allow engine initialisation to fail by marking the GPU as
  3762. * wedged. But we only want to do this where the GPU is angry,
  3763. * for all other failure, such as an allocation failure, bail.
  3764. */
  3765. DRM_ERROR("Failed to initialize GPU, declaring it wedged\n");
  3766. i915_gem_set_wedged(dev_priv);
  3767. ret = 0;
  3768. }
  3769. out_unlock:
  3770. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  3771. mutex_unlock(&dev->struct_mutex);
  3772. return ret;
  3773. }
  3774. void
  3775. i915_gem_cleanup_engines(struct drm_device *dev)
  3776. {
  3777. struct drm_i915_private *dev_priv = to_i915(dev);
  3778. struct intel_engine_cs *engine;
  3779. for_each_engine(engine, dev_priv)
  3780. dev_priv->gt.cleanup_engine(engine);
  3781. }
  3782. static void
  3783. init_engine_lists(struct intel_engine_cs *engine)
  3784. {
  3785. INIT_LIST_HEAD(&engine->request_list);
  3786. }
  3787. void
  3788. i915_gem_load_init_fences(struct drm_i915_private *dev_priv)
  3789. {
  3790. struct drm_device *dev = &dev_priv->drm;
  3791. int i;
  3792. if (INTEL_INFO(dev_priv)->gen >= 7 && !IS_VALLEYVIEW(dev_priv) &&
  3793. !IS_CHERRYVIEW(dev_priv))
  3794. dev_priv->num_fence_regs = 32;
  3795. else if (INTEL_INFO(dev_priv)->gen >= 4 || IS_I945G(dev_priv) ||
  3796. IS_I945GM(dev_priv) || IS_G33(dev_priv))
  3797. dev_priv->num_fence_regs = 16;
  3798. else
  3799. dev_priv->num_fence_regs = 8;
  3800. if (intel_vgpu_active(dev_priv))
  3801. dev_priv->num_fence_regs =
  3802. I915_READ(vgtif_reg(avail_rs.fence_num));
  3803. /* Initialize fence registers to zero */
  3804. for (i = 0; i < dev_priv->num_fence_regs; i++) {
  3805. struct drm_i915_fence_reg *fence = &dev_priv->fence_regs[i];
  3806. fence->i915 = dev_priv;
  3807. fence->id = i;
  3808. list_add_tail(&fence->link, &dev_priv->mm.fence_list);
  3809. }
  3810. i915_gem_restore_fences(dev);
  3811. i915_gem_detect_bit_6_swizzle(dev);
  3812. }
  3813. void
  3814. i915_gem_load_init(struct drm_device *dev)
  3815. {
  3816. struct drm_i915_private *dev_priv = to_i915(dev);
  3817. int i;
  3818. dev_priv->objects =
  3819. kmem_cache_create("i915_gem_object",
  3820. sizeof(struct drm_i915_gem_object), 0,
  3821. SLAB_HWCACHE_ALIGN,
  3822. NULL);
  3823. dev_priv->vmas =
  3824. kmem_cache_create("i915_gem_vma",
  3825. sizeof(struct i915_vma), 0,
  3826. SLAB_HWCACHE_ALIGN,
  3827. NULL);
  3828. dev_priv->requests =
  3829. kmem_cache_create("i915_gem_request",
  3830. sizeof(struct drm_i915_gem_request), 0,
  3831. SLAB_HWCACHE_ALIGN |
  3832. SLAB_RECLAIM_ACCOUNT |
  3833. SLAB_DESTROY_BY_RCU,
  3834. NULL);
  3835. INIT_LIST_HEAD(&dev_priv->context_list);
  3836. INIT_LIST_HEAD(&dev_priv->mm.unbound_list);
  3837. INIT_LIST_HEAD(&dev_priv->mm.bound_list);
  3838. INIT_LIST_HEAD(&dev_priv->mm.fence_list);
  3839. for (i = 0; i < I915_NUM_ENGINES; i++)
  3840. init_engine_lists(&dev_priv->engine[i]);
  3841. INIT_DELAYED_WORK(&dev_priv->gt.retire_work,
  3842. i915_gem_retire_work_handler);
  3843. INIT_DELAYED_WORK(&dev_priv->gt.idle_work,
  3844. i915_gem_idle_work_handler);
  3845. init_waitqueue_head(&dev_priv->gpu_error.wait_queue);
  3846. init_waitqueue_head(&dev_priv->gpu_error.reset_queue);
  3847. init_waitqueue_head(&dev_priv->pending_flip_queue);
  3848. dev_priv->mm.interruptible = true;
  3849. atomic_set(&dev_priv->mm.bsd_engine_dispatch_index, 0);
  3850. spin_lock_init(&dev_priv->fb_tracking.lock);
  3851. }
  3852. void i915_gem_load_cleanup(struct drm_device *dev)
  3853. {
  3854. struct drm_i915_private *dev_priv = to_i915(dev);
  3855. kmem_cache_destroy(dev_priv->requests);
  3856. kmem_cache_destroy(dev_priv->vmas);
  3857. kmem_cache_destroy(dev_priv->objects);
  3858. /* And ensure that our DESTROY_BY_RCU slabs are truly destroyed */
  3859. rcu_barrier();
  3860. }
  3861. int i915_gem_freeze(struct drm_i915_private *dev_priv)
  3862. {
  3863. intel_runtime_pm_get(dev_priv);
  3864. mutex_lock(&dev_priv->drm.struct_mutex);
  3865. i915_gem_shrink_all(dev_priv);
  3866. mutex_unlock(&dev_priv->drm.struct_mutex);
  3867. intel_runtime_pm_put(dev_priv);
  3868. return 0;
  3869. }
  3870. int i915_gem_freeze_late(struct drm_i915_private *dev_priv)
  3871. {
  3872. struct drm_i915_gem_object *obj;
  3873. struct list_head *phases[] = {
  3874. &dev_priv->mm.unbound_list,
  3875. &dev_priv->mm.bound_list,
  3876. NULL
  3877. }, **p;
  3878. /* Called just before we write the hibernation image.
  3879. *
  3880. * We need to update the domain tracking to reflect that the CPU
  3881. * will be accessing all the pages to create and restore from the
  3882. * hibernation, and so upon restoration those pages will be in the
  3883. * CPU domain.
  3884. *
  3885. * To make sure the hibernation image contains the latest state,
  3886. * we update that state just before writing out the image.
  3887. *
  3888. * To try and reduce the hibernation image, we manually shrink
  3889. * the objects as well.
  3890. */
  3891. mutex_lock(&dev_priv->drm.struct_mutex);
  3892. i915_gem_shrink(dev_priv, -1UL, I915_SHRINK_UNBOUND);
  3893. for (p = phases; *p; p++) {
  3894. list_for_each_entry(obj, *p, global_list) {
  3895. obj->base.read_domains = I915_GEM_DOMAIN_CPU;
  3896. obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  3897. }
  3898. }
  3899. mutex_unlock(&dev_priv->drm.struct_mutex);
  3900. return 0;
  3901. }
  3902. void i915_gem_release(struct drm_device *dev, struct drm_file *file)
  3903. {
  3904. struct drm_i915_file_private *file_priv = file->driver_priv;
  3905. struct drm_i915_gem_request *request;
  3906. /* Clean up our request list when the client is going away, so that
  3907. * later retire_requests won't dereference our soon-to-be-gone
  3908. * file_priv.
  3909. */
  3910. spin_lock(&file_priv->mm.lock);
  3911. list_for_each_entry(request, &file_priv->mm.request_list, client_list)
  3912. request->file_priv = NULL;
  3913. spin_unlock(&file_priv->mm.lock);
  3914. if (!list_empty(&file_priv->rps.link)) {
  3915. spin_lock(&to_i915(dev)->rps.client_lock);
  3916. list_del(&file_priv->rps.link);
  3917. spin_unlock(&to_i915(dev)->rps.client_lock);
  3918. }
  3919. }
  3920. int i915_gem_open(struct drm_device *dev, struct drm_file *file)
  3921. {
  3922. struct drm_i915_file_private *file_priv;
  3923. int ret;
  3924. DRM_DEBUG_DRIVER("\n");
  3925. file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL);
  3926. if (!file_priv)
  3927. return -ENOMEM;
  3928. file->driver_priv = file_priv;
  3929. file_priv->dev_priv = to_i915(dev);
  3930. file_priv->file = file;
  3931. INIT_LIST_HEAD(&file_priv->rps.link);
  3932. spin_lock_init(&file_priv->mm.lock);
  3933. INIT_LIST_HEAD(&file_priv->mm.request_list);
  3934. file_priv->bsd_engine = -1;
  3935. ret = i915_gem_context_open(dev, file);
  3936. if (ret)
  3937. kfree(file_priv);
  3938. return ret;
  3939. }
  3940. /**
  3941. * i915_gem_track_fb - update frontbuffer tracking
  3942. * @old: current GEM buffer for the frontbuffer slots
  3943. * @new: new GEM buffer for the frontbuffer slots
  3944. * @frontbuffer_bits: bitmask of frontbuffer slots
  3945. *
  3946. * This updates the frontbuffer tracking bits @frontbuffer_bits by clearing them
  3947. * from @old and setting them in @new. Both @old and @new can be NULL.
  3948. */
  3949. void i915_gem_track_fb(struct drm_i915_gem_object *old,
  3950. struct drm_i915_gem_object *new,
  3951. unsigned frontbuffer_bits)
  3952. {
  3953. /* Control of individual bits within the mask are guarded by
  3954. * the owning plane->mutex, i.e. we can never see concurrent
  3955. * manipulation of individual bits. But since the bitfield as a whole
  3956. * is updated using RMW, we need to use atomics in order to update
  3957. * the bits.
  3958. */
  3959. BUILD_BUG_ON(INTEL_FRONTBUFFER_BITS_PER_PIPE * I915_MAX_PIPES >
  3960. sizeof(atomic_t) * BITS_PER_BYTE);
  3961. if (old) {
  3962. WARN_ON(!(atomic_read(&old->frontbuffer_bits) & frontbuffer_bits));
  3963. atomic_andnot(frontbuffer_bits, &old->frontbuffer_bits);
  3964. }
  3965. if (new) {
  3966. WARN_ON(atomic_read(&new->frontbuffer_bits) & frontbuffer_bits);
  3967. atomic_or(frontbuffer_bits, &new->frontbuffer_bits);
  3968. }
  3969. }
  3970. /* Like i915_gem_object_get_page(), but mark the returned page dirty */
  3971. struct page *
  3972. i915_gem_object_get_dirty_page(struct drm_i915_gem_object *obj, int n)
  3973. {
  3974. struct page *page;
  3975. /* Only default objects have per-page dirty tracking */
  3976. if (WARN_ON(!i915_gem_object_has_struct_page(obj)))
  3977. return NULL;
  3978. page = i915_gem_object_get_page(obj, n);
  3979. set_page_dirty(page);
  3980. return page;
  3981. }
  3982. /* Allocate a new GEM object and fill it with the supplied data */
  3983. struct drm_i915_gem_object *
  3984. i915_gem_object_create_from_data(struct drm_device *dev,
  3985. const void *data, size_t size)
  3986. {
  3987. struct drm_i915_gem_object *obj;
  3988. struct sg_table *sg;
  3989. size_t bytes;
  3990. int ret;
  3991. obj = i915_gem_object_create(dev, round_up(size, PAGE_SIZE));
  3992. if (IS_ERR(obj))
  3993. return obj;
  3994. ret = i915_gem_object_set_to_cpu_domain(obj, true);
  3995. if (ret)
  3996. goto fail;
  3997. ret = i915_gem_object_get_pages(obj);
  3998. if (ret)
  3999. goto fail;
  4000. i915_gem_object_pin_pages(obj);
  4001. sg = obj->pages;
  4002. bytes = sg_copy_from_buffer(sg->sgl, sg->nents, (void *)data, size);
  4003. obj->dirty = 1; /* Backing store is now out of date */
  4004. i915_gem_object_unpin_pages(obj);
  4005. if (WARN_ON(bytes != size)) {
  4006. DRM_ERROR("Incomplete copy, wrote %zu of %zu", bytes, size);
  4007. ret = -EFAULT;
  4008. goto fail;
  4009. }
  4010. return obj;
  4011. fail:
  4012. i915_gem_object_put(obj);
  4013. return ERR_PTR(ret);
  4014. }